Drop Empty Columns in Pandas - GeeksforGeeks

23 Dec, 2020
In this article, we will try to see different ways of removing the Empty column, Null column, and zeros value column. First, We will create a sample data frame and then we will perform our operations in subsequent examples by the end you will get a strong hand knowledge on how to handle this situation with pandas.
Approach:
Import required python library.
Create a sample Data Frame.
Use the Pandas dropna() method, It allows the user to analyze and drop Rows/Columns with Null values in different ways.
Display updated Data Frame.
Syntax: DataFrameName.dropna(axis=0, how=’any’, inplace=False)
Parameters:
axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String.
how: how takes string value of two kinds only (‘any’ or ‘all’). ‘any’ drops the row/column if ANY value is Null and ‘all’ drops only if ALL values are null.
inplace: It is a boolean which makes the changes in the data frame itself if True.
Sample Data:
This is the sample data frame on which we will perform different operations.
Python3
# import required librariesimport numpy as npimport pandas as pd  # create a DataframeMydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],                            "Gender": ["", "", ""],                            "Age": [0, 0, 0]})Mydataframe['Department'] = np.nan  # show the dataframeprint(Mydataframe)
Output:
Example 1:
Remove all null value column.
Python3
# import required librariesimport numpy as npimport pandas as pd  # create a DataframeMydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],                            "Gender": ["", "", ""],                            "Age": [0, 0, 0]})  Mydataframe['Department'] = np.nan  display(Mydataframe)  Mydataframe.dropna(how='all', axis=1, inplace=True)  # show the dataframedisplay(Mydataframe)
Output:
Example 2:
Replace all Empty places with null and then Remove all null values column with dropna function.
Python3
# import required librariesimport numpy as npimport pandas as pd  # create a DataframeMydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],                            "Gender": ["", "", ""],                            "Age": [0, 0, 0]})  Mydataframe['Department'] = np.nandisplay(Mydataframe)  nan_value = float("NaN")Mydataframe.replace("", nan_value, inplace=True)  Mydataframe.dropna(how='all', axis=1, inplace=True)  # show the dataframedisplay(Mydataframe)
Output:
Example 3:
Replace all zeros places with null and then Remove all null values column with dropna function.
Python3
# import required librariesimport numpy as npimport pandas as pd  # create a DataframeMydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],                            "Gender": ["", "", ""],                            "Age": [0, 0, 0]})  Mydataframe['Department'] = np.nandisplay(Mydataframe)  nan_value = float("NaN")Mydataframe.replace(0, nan_value, inplace=True)  Mydataframe.dropna(how='all', axis=1, inplace=True)  # show the dataframedisplay(Mydataframe)
Output:
Example 4:
Replace all zeros and empty places with null and then Remove all null values column with dropna function.
Python3
# import required librariesimport numpy as npimport pandas as pd  # create a DataframeMydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],                            "Gender": ["", "", ""],                            "Age": [0, 0, 0]})  Mydataframe['Department'] = np.nandisplay(Mydataframe)  nan_value = float("NaN")Mydataframe.replace(0, nan_value, inplace=True)Mydataframe.replace("", nan_value, inplace=True)  Mydataframe.dropna(how='all', axis=1, inplace=True)  # show the dataframedisplay(Mydataframe)
Output:
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()