Python Pandas program to replace all the NaN values with a scaler in a column of a Dataframe

In this python pandas program, we will replace all the NaN values with a scaler in a column using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import NumPy library as np.
  3. Create a dataframe using pd.DataFrame().
  4. Replace all the NaN values with a scaler in a column of a Dataframe using df.fillna(value = 25,inplace = True).
  5. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,np.nan,29,np.nan]}
df = pd.DataFrame(d)
print(df)
df.fillna(value = 25,inplace = True)
print("After filling nan values: \n",df)
				
			

Output :

				
					0   Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
After filling nan values: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  25.0
2       3  Peter  29.0
3       4  Klaus  25.0
				
			

count Country wise population from a given data set

count the NaN values in a Dataframe

Leave a Comment