Python Pandas program to replace NaNs with the median value in a DataFrame

In this python pandas program, we will replace NaNs with the median value using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values in the age column with the median value of the same column using df[‘Age’].fillna(df[‘Age’].median(),inplace=True).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
df['Age'].fillna(df['Age'].median(),inplace=True)
print("After replacing missing values by mean: \n",df)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
After replacing missing values by mean: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  29.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace the missing values with the most frequent values present in each column of a given DataFrame

import a CSV file

Python Pandas program to replace the NaN values with mode value in a DataFrame

In this python pandas program, we will replace the NaN values with mode value in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values with the mode value in of the same column using df.fillna(method=’pad’).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4,5],
                   'Name':['Alex',np.nan,'Peter','Klaus','Stefan'],
                   'Age':[30,np.nan,29,22,22]})
print("Original Dataframe: \n",df)
result = df.fillna(df.mode().iloc[0])
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.    Name   Age
0       1    Alex  30.0
1       2     NaN   NaN
2       3   Peter  29.0
3       4   Klaus  22.0
4       5  Stefan  22.0
   Sr.no.    Name   Age
0       1    Alex  30.0
1       2    Alex  22.0
2       3   Peter  29.0
3       4   Klaus  22.0
4       5  Stefan  22.0
				
			

replace NaNs with mean in a DataFrame

replace NaNs with the median value in a DataFrame

Python Pandas program to replace NaNs with mean in a DataFrame

In this python pandas program, we will replace NaNs with mean in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Replace the NaN values in the age column with the mean age using df[‘Age’].fillna(df[‘Age’].mean(),inplace=True).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
df['Age'].fillna(df['Age'].mean(),inplace=True)
print("After replacing missing values by mean: \n",df)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
After replacing missing values by mean: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  27.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace NaNs with the value from the next row in a DataFrame

replace the missing values with the most frequent values present in each column of a given DataFrame

Replace NaNs with the value from the next row in a DataFrame

In this python pandas program, we will replace NaNs with the value from the next row in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values with the value in the next row of the same column using df.fillna(method=’bfill’).
  4. Print the output
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex',np.nan,'Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
print("Fill the rows where all elements are missing with next values:")
result = df.fillna(method='bfill')
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2    NaN   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
Fill the rows where all elements are missing with next values:
   Sr.no.   Name   Age
0       1   Alex  30.0
1       2  Peter  29.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace NaNs with the value from the previous row in a DataFrame

replace NaNs with mean in a DataFrame

Replace NaNs with the value from the previous row in a DataFrame

In this python pandas program, we will replace NaNs with the value from the previous row in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values with the value in the previous row of the same column using df.fillna(method=’pad’).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex',np.nan,'Peter','Klaus'],
                   'Age':[30,np.nan,29,np.nan]})
print("Original Dataframe: \n",df)
print("Fill the rows where all elements are missing with previous values:")
result = df.fillna(method='pad')
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2    NaN   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
Fill the rows where all elements are missing with previous values:
   Sr.no.   Name   Age
0       1   Alex  30.0
1       2   Alex  30.0
2       3  Peter  29.0
3       4  Klaus  29.0
				
			

drop the rows where all elements are missing in a DataFrame

replace NaNs with the value from the next row in a DataFrame

Drop the rows where all elements are missing in a DataFrame

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
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now drop the rows where all elements are missing using df.dropna(how=’all’).
  4. 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
				
			

drop the columns where at least one element is missing in a DataFrame

replace NaNs with the value from the previous row in a DataFrame

Drop the columns where at least one element is missing in a DataFrame

In this python pandas program, we will drop the columns where at least one element is missing using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now drop the columns where at least one element is missing using df.dropna(axis=1).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex',np.nan,'Peter','Klaus'],
                   'Age':[30,np.nan,29,np.nan]})
print("Original Dataframe: \n",df)
result = df.dropna(axis=1)
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2    NaN   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
   Sr.no.
0       1
1       2
2       3
3       4
				
			

drop the rows where at least one element is missing in a DataFrame

drop the rows where all elements are missing in a DataFrame

Drop the rows where at least one element is missing in a DataFrame

In this python pandas program, we will Drop the rows where at least one element is missing in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now drop the rows from the DataFrame where at least one element is missing using df.dropna().
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex',np.nan,'Peter','Klaus'],
                   'Age':[30,np.nan,29,np.nan]})
print("Original Dataframe: \n",df)
result = df.dropna()
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2    NaN   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
   Sr.no.   Name   Age
0       1   Alex  30.0
2       3  Peter  29.0
				
			

count the number of missing values in each column of a DataFrame

drop the columns where at least one element is missing in a DataFrame