Python Pandas Problems
Python Pandas Problems
Python Pandas program to import a CSV file
In this python pandas program, we will import a CSV file using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Now import a CSV file using pd.read_csv(“file name”).
- Then print the output.
import pandas as pd
df = pd.read_csv("file name")
print(df)
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- 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).
- 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
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Now replace the NaN values with the mode value in of the same column using df.fillna(method=’pad’).
- 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
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Replace the NaN values in the age column with the mean age using df[‘Age’].fillna(df[‘Age’].mean(),inplace=True).
- 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
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Now replace the NaN values with the value in the next row of the same column using df.fillna(method=’bfill’).
- 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
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Now replace the NaN values with the value in the previous row of the same column using df.fillna(method=’pad’).
- 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
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
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Now drop the columns where at least one element is missing using df.dropna(axis=1).
- 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
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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Now drop the rows from the DataFrame where at least one element is missing using df.dropna().
- 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