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

Python Pandas program to replace all the NaN values with a scaler in a column of a Dataframe

In this python pandas program, we will replace all the NaN values with a scaler in a column 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. Replace all the NaN values with a scaler in a column of a Dataframe using df.fillna(value = 25,inplace = True).
  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)
df.fillna(value = 25,inplace = True)
print("After filling nan values: \n",df)
				
			

Output :

				
					0   Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
After filling nan values: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  25.0
2       3  Peter  29.0
3       4  Klaus  25.0
				
			

count Country wise population from a given data set

count the NaN values in a Dataframe

Python Pandas program to count Country wise population from a given dataset

In this python pandas program, we will count Country wise population from a given dataset using the pandas library.

Link for Dataset- https://www.kaggle.com/datasets/tanuprabhu/population-by-country-2020?resource=download

Steps to solve the program
  1. Import pandas library as pd.
  2. First, read the dataset using pd.read_csv().
  3. Now drop the unnecessary columns from the dataset using df.drop().
  4. Print the first 10 countries using df.head(10).
				
					import pandas as pd
df = pd.read_csv("population_by_country_2020.csv")
df1 = df.drop(['Yearly Change','Net Change','Density (P/Km²)','Land Area (Km²)',
              'Migrants (net)','Fert. Rate','Med. Age','Urban Pop %','World Share'],axis=1)
print(df1.head(10))
				
			

Output :

				
					0  Country (or dependency)  Population (2020)
0                   China         1440297825
1                   India         1382345085
2           United States          331341050
3               Indonesia          274021604
4                Pakistan          221612785
5                  Brazil          212821986
6                 Nigeria          206984347
7              Bangladesh          164972348
8                  Russia          145945524
9                  Mexico          129166028
				
			

write a DataFrame to a CSV file using a tab separato

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

Program to write a DataFrame to a CSV file

In this python pandas program, we will write a DataFrame to a CSV file using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Write the dataFrame to a CSV file using a tab separator using df.to_csv(‘new_file.csv’, sep=’\t’, index=False)
  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)
df.to_csv('new_file.csv', sep='\t', index=False)
new = pd.read_csv('new_file.csv')
print(new)
				
			

Output :

				
					0  Sr.no.\tName\tAge\tSalary
0        1\tAlex\t30\t50000
1        2\tJohn\t27\t65000
2       3\tPeter\t29\t58000
3       4\tKlaus\t33\t66000
				
			

change the order of columns in a DataFrame

count Country wise population from a given data set

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

In this python pandas program, we will change 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. Change the order of columns in a DataFrame using df[[‘Sr.no.’,’Name’,’Salary’,’Age’]].
  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("Original Dataframe: \n",df)
df = df[['Sr.no.','Name','Salary','Age']]
print('After re-ordering columns: \n',df)
				
			

Output :

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

rename columns of a given DataFrame

write a DataFrame to a CSV file using a tab separator