Python Pandas program to find the index of a column from the DataFrame

In this python pandas program, we will find the index of a column from the DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Find the index of the age column in the dataframe using df.columns.get_loc(‘Age’).
  4. 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,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
index = df.columns.get_loc('Age')
print("Index no of age column: ",index)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Index no of age column:  2
				
			

convert a list of lists into a Dataframe

select all columns except one column in a DataFrame

Python Pandas program to convert a list of lists into a Dataframe

In this python pandas program, we will convert a list of lists into a Dataframe using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a list of lists.
  3. Now convert the list of lists into a dataframe using pd.DataFrame().
  4. Print the output.
				
					import pandas as pd
l = [['Virat','cricket'],['Messi','football']]
df = pd.DataFrame(l)
print(df)
				
			

Output :

				
					0       0         1
0  Virat   cricket
1  Messi  football
				
			

get the datatypes of columns of a DataFrame

find the index of a column from the DataFrame

Python Pandas program to get the datatypes of columns of a DataFrame

In this python pandas program, we will get the datatypes of columns of 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. Get the datatypes of columns of a DataFrame using df.types.
  4. 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,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print(df.dtypes)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Sr.no.     int64
Name      object
Age        int64
Salary     int64
dtype: object
				
			

check whether a column is present in a DataFrame

convert a list of lists into a Dataframe

Python Pandas program to check whether a column is present in a DataFrame

In this python pandas program, we will check whether a column is present 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. Take a list of some columns.
  4. Use for loop to iterate over the list of columns and an if-else statement with df.columns to check whether a column is present in a DataFrame or not.
  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,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
for i in ["Company",'Name']:
    if i in df.columns:
        print(f"{i} is present in DataFrame.")
    else:
        print(f"{i} is not present in DataFrame.")
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Company is not present in DataFrame.
Name is present in DataFrame.
				
			

find the row where the value of a given column is minimum

get the datatypes of columns of a DataFrame

Python Pandas program to find the row where the value of a column is minimum

In this python pandas program, we will row where the value of a column is minimum using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Find the row where the value in the salary column is minimum using df[‘Salary’].argmin().
  4. 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,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("Row where Salary has minimum value:")
print(df['Salary'].argmin())
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Row where Salary has minimum value:
0
				
			

find the row where the value of a column is maximum

check whether a column is present in a DataFrame

Python Pandas program to find the row where the value of a column is maximum

In this python pandas program, we will find the row where the value of a column is maximum using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. FInd the row which has maximum value in the salary column using df[‘Salary’].argmax().
  4. 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,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("Row where Salary has maximum value:")
print(df['Salary'].argmax())
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Row where Salary has maximum value:
3
				
			

get a list of records in a  column of a DataFrame

find the row where the value of a given column is minimum

Python Pandas program to get a list of records in a column of a DataFrame

In this python pandas program, we will get a list of records in a  column of 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. Get a list of records in the Name columns using df[‘Name’].tolist().
  4. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
name_list = df['Name'].tolist()
print("List of names: ",name_list)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
List of names:  ['Alex', 'John', 'Peter', 'Klaus']
				
			

rename a column in a DataFrame

find the row where the value of a column is maximum

Python Pandas program to rename a column in a DataFrame

In this python pandas program, we will rename a column in a DataFrame using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Rename the Rank column to Sr.No. using df.rename(columns = {‘Rank’:’Sr.no.’}).
  4. Print the output.
				
					import pandas as pd
d = {'Rank':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
df = df.rename(columns = {'Rank':'Sr.no.'})
print("New: \n",df)
				
			

Output :

				
					0   Rank   Name  Age  Salary
0     1   Alex   30   50000
1     2   John   27   65000
2     3  Peter   29   58000
3     4  Klaus   33   66000
New: 
    Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
				
			

shuffle rows in a DataFrame

get a list of records in a  column of a DataFrame

Python Pandas program to shuffle rows in a DataFrame

In this python pandas program, we will shuffle rows 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. Shuffle rows in a Dataframe using df.sample(frac=1).
  4. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
df = df.sample(frac=1)
print("\nNew DataFrame:")
print(df)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000

New DataFrame:
   Sr.no.   Name  Age  Salary
3       4  Klaus   33   66000
2       3  Peter   29   58000
1       2   John   27   65000
0       1   Alex   30   50000
				
			

count the NaN values in a Dataframe

rename a column in a DataFrame

Python Pandas program to count the NaN values in a Dataframe

In this python pandas program, we will count the NaN values in a Dataframe 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. Count the NaN values in a Dataframe using df.isnull().values.sum().
  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)
print("Nan values in the dataframe: ",df.isnull().values.sum())
				
			

Output :

				
					0   Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
Nan values in the dataframe:  2

				
			

replace all the NaN values with a scaler in a column of a Dataframe

shuffle rows in a DataFrame