pandas Missing Data

Estimated reading: 1 minute 324 views

Missing Data

Using fillna and dropna we can handling missing data.

				
					# using dropna we can drop the data whcih will have nan

import numpy as np
import pandas as pd
df = pd.DataFrame({'A':[1,2,np.nan],
                  'B':[5,np.nan,np.nan],
                  'C':[1,2,3]})
                  
df.dropna() # which will drop The nan rows
df.dropna(axis=1) # which will drop the nan columns
df.dropna(thresh=2) # it will drop those row which will 2 or more nan values
				
			
				
					# using fillna we can fill the replace the nan on given value in fillna 

import numpy as np
import pandas as pd
df = pd.DataFrame({'A':[1,2,np.nan],
                  'B':[5,np.nan,np.nan],
                  'C':[1,2,3]})
df.fillna(value='FILL VALUE')
df['A'].fillna(value=df['A'].mean())
                  

				
			

Leave a Comment

CONTENTS