In this python pandas program, we will drop the rows where all elements are missing in a DataFrame using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Now drop the rows where all elements are missing using df.dropna(how=’all’).
- Print the output.
import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,np.nan,3,4],
'Name':['Alex',np.nan,'Peter','Klaus'],
'Age':[30,np.nan,29,np.nan]})
print("Original Dataframe: \n",df)
print("Drop the rows where all elements are missing:")
result = df.dropna(how='all')
print(result)
Output :
Original Dataframe:
Sr.no. Name Age
0 1.0 Alex 30.0
1 NaN NaN NaN
2 3.0 Peter 29.0
3 4.0 Klaus NaN
Drop the rows where all elements are missing:
Sr.no. Name Age
0 1.0 Alex 30.0
2 3.0 Peter 29.0
3 4.0 Klaus NaN