Python Pandas program to remove the first n rows of a DataFrame

In this python pandas program, w will remove the first n rows 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. Create a new dataframe by after removing the first 2 rows using df1 = df.iloc[2:].
  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("After removing first 2 rows of the DataFrame:")
df1 = df.iloc[2:]
print(df1)
				
			

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
After removing first 2 rows of the DataFrame:
   Sr.no.   Name  Age  Salary
2       3  Peter   29   58000
3       4  Klaus   33   66000
				
			

select all columns except one column in a DataFrame

remove the last n rows of a given DataFrame

Python Pandas program to select all columns except one column in a DataFrame 

In this python pandas program, we will select all columns except one column 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. Create a new dataframe that has all columns except the age column using df.loc[:, df.columns != ‘Age’].
  4. It will select all the rows for all columns except the age column.
  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)
print("All columns except 'Age':")
df = df.loc[:, df.columns != 'Age']
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
All columns except 'Age':
   Sr.no.   Name  Salary
0       1   Alex   50000
1       2   John   65000
2       3  Peter   58000
3       4  Klaus   66000
				
			

find the index of a column from the DataFrame

remove the first n rows of a DataFrame

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