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

Leave a Comment