Python Pandas program to select columns by the object datatype from the DataFrame

In this python pandas program, we will select columns by the object datatype 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. Select columns by the object datatype from the DataFrame using.select_dtypes(include = “object”).
  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("Select string columns")
print(df.select_dtypes(include = "object"))
				
			

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
Select string columns
    Name
0   Alex
1   John
2  Peter
3  Klaus
				
			

reverse the order of columns in a DataFrame

Python Pandas program to reverse the order of columns in a DataFrame

In this python pandas program, we will reverse the order of columns 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. Reverse the order of columns in the dataframe using df.loc[:,::-1].
  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("Reverse column order:")
print(df.loc[:,::-1])
				
			

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
Reverse column order:
   Salary  Age   Name  Sr.no.
0   50000   30   Alex       1
1   65000   27   John       2
2   58000   29  Peter       3
3   66000   33  Klaus       4
				
			

reverse the order of rows of a given DataFrame

select columns by the object datatype from the DataFrame

Python Pandas program to reverse the order of rows of a given DataFrame

In this python pandas program,we will reverse the order of rows of a given DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Reverse the order of rows in the dataframe using df.loc[::-1].
  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("Reverse row order:")
print(df.loc[::-1])
				
			

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
Reverse row order:
   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
				
			

remove the last n rows of a given DataFrame

reverse the order of columns in a DataFrame

Python Pandas program to remove the last n rows of a given DataFrame

In this python pandas program, we will remove the last n rows of a given 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 last 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 last 2 rows of the DataFrame:
   Sr.no.  Name  Age  Salary
0       1  Alex   30   50000
1       2  John   27   65000
				
			

remove the first n rows of a DataFrame

reverse the order of rows of a given DataFrame

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