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

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

In this python pandas program, we will count the number of missing values in each column of a DataFrame.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Count the number of missing values in each column of a DataFrame using df.isna().sum().
  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("Identify the columns which have at least one missing value:")
print(df.isna().sum())
				
			

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
Identify the columns which have at least one missing value:
Sr.no.    0
Name      1
Age       2
dtype: int64
				
			

identify the columns from the DataFrame which have at least one missing value

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

Identify the columns from the DataFrame which have at least one missing value

In this python pandas program, we will identify the columns from the DataFrame which have at least one missing value using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Identify the columns from the DataFrame which have at least one missing value using df.isna().any().
  4. It will print True in front of the column name if that column has any missing value or else False.
  5. 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,np.nan]})
print("Original Dataframe: \n",df)
print("Identify the columns which have at least one missing value:")
print(df.isna().any())
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
Identify the columns which have at least one missing value:
Sr.no.    False
Name      False
Age        True
dtype: bool
				
			

detect missing values from a  DataFrame

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

Python Pandas program to detect and display missing values from a DataFrame

In this python pandas program, we will detect and display missing values from 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. Detect and display missing values from a DataFrame using df.isna().
  4. It will show True if the value is missing else False.
  5. 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,np.nan]})
print("Original Dataframe: \n",df)
print(df.isna())
				
			

Output :

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

merge two Dataframes with different columns

identify the columns from the DataFrame which have at least one missing value

Python Pandas program to merge two Dataframes with different columns

In this python pandas program, we will merge two Dataframes with different columns using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two dataframes using pd.DataFrame().
  3. Merge two Dataframes with different columns using pd.concat([df1,df2], axis=0, ignore_index=True).
  4. pd.concat() will merge the two dataframe but by setting the axis=0 and ignore_index=True it will merge two Dataframes with different columns.
  5. It will show NaN if there is no record of a value in a column.
  6. Print the output.
				
					import pandas as pd
df1 = pd.DataFrame({'Id':['S1','S2','S3'],
                   'Name':['Ketan','Yash','Abhishek'],
                   'Marks':[90,87,77]})
df2 = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,27,29,33]})
print('Dataframe 1: \n',df1)
print('Dataframe 2: \n',df2)
print("Merge two dataframes with different columns:")
result = pd.concat([df1,df2], axis=0, ignore_index=True)
print(result)
				
			

Output :

				
					Dataframe 1: 
    Id      Name  Marks
0  S1     Ketan     90
1  S2      Yash     87
2  S3  Abhishek     77
Dataframe 2: 
    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
Merge two dataframes with different columns:
    Id      Name  Marks  Sr.no.   Age
0   S1     Ketan   90.0     NaN   NaN
1   S2      Yash   87.0     NaN   NaN
2   S3  Abhishek   77.0     NaN   NaN
3  NaN      Alex    NaN     1.0  30.0
4  NaN      John    NaN     2.0  27.0
5  NaN     Peter    NaN     3.0  29.0
6  NaN     Klaus    NaN     4.0  33.0
				
			

join the two Dataframes using the common column of both Dataframes

detect missing values from a  DataFrame

Join the two Dataframes using the common column and value

In this python pandas program, we will Join the two Dataframes using the common column and value using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two dataframes using pd.DataFrame().
  3. We will perform inner join to join the two Dataframes using the common column and value using pd.merge(df1, df2, on=’Id’, how=’inner’).
  4. We will perform an inner join on the id column.
  5. Print the output.
				
					import pandas as pd
df1 = pd.DataFrame({'Id':['S1','S2','S3'],
                   'Name':['Ketan','Yash','Abhishek'],
                   'Marks':[90,87,77]})
df2 = pd.DataFrame({'Id':['S2','S4'],
                    'Name':['Yash','Gaurav'],
                   'Marks':[70,65]})
print('Dataframe 1: \n',df1)
print('Dataframe 2: \n',df2)
new = pd.merge(df1, df2, on='Id', how='inner')
print("Merged data:")
print(new)
				
			

Output :

				
					Dataframe 1: 
    Id      Name  Marks
0  S1     Ketan     90
1  S2      Yash     87
2  S3  Abhishek     77
Dataframe 2: 
    Id    Name  Marks
0  S2    Yash     70
1  S4  Gaurav     65
Merged data:
   Id Name_x  Marks_x Name_y  Marks_y
0  S2   Yash       87   Yash       70
				
			

join the two Dataframes along columns

merge two Dataframes with different columns

Python Pandas program to join the two Dataframes along columns

In this python pandas program, we will join the two Dataframes along columns using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two dataframes using pd.DataFrame().
  3. Join the dataframes along the columns using pd.concat() and set axis=1.
  4. Print the output.
				
					import pandas as pd
df1 = pd.DataFrame({'ID':[1,2,3],'Name':['Yash','Gaurav','Sanket'],
                   'Age':[30,27,28]})
df2 = pd.DataFrame({'ID':[4,3],'Name':['Tanmay','Athrva'],'Age':[26,22]})
result = pd.concat([df1,df2],axis=1)
print("New dataframe")
print(result)
				
			

Output :

				
					New dataframe
   ID    Name  Age   ID    Name   Age
0   1    Yash   30  4.0  Tanmay  26.0
1   2  Gaurav   27  3.0  Athrva  22.0
2   3  Sanket   28  NaN     NaN   NaN
				
			

join the two Dataframes along rows

join the two Dataframes using the common column of both Dataframes