pandas remove empty columns code example

Example 1: pandas drop empty columns

DataFrameName.dropna(axis=1, how='all', inplace=True)

Example 2: add empty column to dataframe pandas

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
>>> df
   A  B
0  1  2
1  2  3
2  3  4
>>> df["C"] = ""
>>> df["D"] = np.nan
>>> df
   A  B C   D
0  1  2   NaN
1  2  3   NaN
2  3  4   NaN

Example 3: remove rows or columns with NaN value

df.dropna()     #drop all rows that have any NaN values
df.dropna(how='all')