Python Pandas program to rename columns of a given DataFrame

In this python pandas program, we will rename columns 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. Rename columns of a given DataFrame to A,B,C using df.rename(columns= {‘C1′:’A’,’C2′:’B’,’C3′:’C’}).
  4. Print the output.
				
					import pandas as pd
d = {'C1':[1,3,8],'C2':[6,8,0],'C3':[8,2,6]}
df = pd.DataFrame(d)
print("Old Dataframe: \n",df)
df = df.rename(columns= {'C1':'A','C2':'B','C3':'C'})
print("New DataFrame after renaming columns:")
print(df)
				
			

Output :

				
					Old Dataframe: 
    C1  C2  C3
0   1   6   8
1   3   8   2
2   8   0   6
New DataFrame after renaming columns:
   A  B  C
0  1  6  8
1  3  8  2
2  8  0  6
				
			

get a list of column headers from the DataFrame

change the order of columns in a DataFrame

Python Pandas program to get a list of column headers from the DataFrame

In this python pandas program, we will get a list of column headers 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. Get a list of column headers from the DataFrame using list(df.columns.values).
  4. Print the output.
				
					import pandas as pd
d = {'name':['Virat','Messi','Kobe'],'sport':['cricket','football','basketball']}
df = pd.DataFrame(d)
print("Dataframe: \n",df)
print("Names of columns: ")
print(list(df.columns.values))
				
			

Output :

				
					Dataframe: 
     name       sport
0  Virat     cricket
1  Messi    football
2   Kobe  basketball
Names of columns: 
['name', 'sport']
				
			

iterate over rows in a DataFrame

rename columns of a given DataFrame

Python Pandas program to iterate over rows in a DataFrame

In this python pandas program, we will iterate over rows 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. Iterate over rows in a DataFrame using for loop and df.iterrows().
  4. Print the records for each column using row[column1],row[columns2],……,row[column_n].
				
					import pandas as pd
import numpy as np
d = [{'name':'Yash','percentage':78},{'name':'Rakesh','percentage':80},{'name':'Suresh','percentage':60}]
df = pd.DataFrame(d)
for index, row in df.iterrows():
    print(row['name'], row['percentage'])
				
			

Output :

				
					Yash 78
Rakesh 80
Suresh 60

				
			

add a new column in a DataFrame

get a list of column headers from the DataFrame

Python Pandas program to add a new column in a DataFrame

In this python pandas program, we will add a new 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. Create a list of records for the new Salary column.
  4. Add a Salary column in the given dataframe using df[‘Salary’] = Salary.
  5. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print("Old Series: \n",df)
Salary = [50000,65000,58000,66000]
df['Salary'] = Salary
print("New Series: \n",df)
				
			

Output :

				
					Old Series: 
    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
New Series: 
    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
				
			

delete the record in a DataFrame

iterate over rows in a DataFrame

Python Pandas program to delete records from the DataFrame

In this python pandas program, we will delete records from the DataFrame using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Delete records for John using  df[df.Name != ‘John’].
  4. It retains all records in the dataframe except for John.
  5. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print("Old Series",df)
df = df[df.Name != 'John']
print("New Series")
print(df)
				
			

Output :

				
					Old Series    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
New DataFrame
   Sr.no.   Name  Age
0       1   Alex   30
2       3  Peter   29
3       4  Klaus   33
				
			

replace the value in a row

add a new column in a DataFrame

Python Pandas program to replace a DataFrame value

In this python pandas program, we will replace a DataFrame value using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Replace John with Jim in the DataFrame using df[‘Name’].replace(‘John’,’Jim’).
  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]}
df = pd.DataFrame(d)
print(df)
print("Change the age of John to Jim:")
df['Name'] = df['Name'].replace('John','Jim')
print(df)
				
			

Output :

				
					0   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
Change the age of John to Jim:
   Sr.no.   Name  Age
0       1   Alex   30
1       2    Jim   27
2       3  Peter   29
3       4  Klaus   33
				
			

sort the DataFrame by ‘age’ column in ascending order

delete a record from the DataFrame

Python Pandas program to sort the DataFrame by a column

In this python pandas program, we will sort the DataFrame by a column using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Sort the DataFrame by age column in ascending order using df.sort_values(by=[‘Age’], ascending=[True]).
  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]}
df = pd.DataFrame(d)
print("Original Series: ")
print(df)
new = df.sort_values(by=['Age'], ascending=[True])
print("After sorting: ")
print(new)
				
			

Output :

				
					Original Series: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
After sorting: 
   Sr.no.   Name  Age
1       2   John   27
2       3  Peter   29
0       1   Alex   30
3       4  Klaus   33
				
			

calculate the mean of age column

replace the value in a row

Python Pandas program to calculate the mean of a column

In this python pandas program, we will calculate the mean of a column using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Calculate the mean of the age column from the DataFrame using df[‘Age’].mean().
  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]}
df = pd.DataFrame(d)
print(df)
print("Sum of age columns: ",df['Age'].mean())
				
			

Output :

				
					0   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
Sum of age columns:  29.75
				
			

sort the DataFrame first by ‘name’ in ascending order

sort the DataFrame by ‘age’ column in ascending order

Python Pandas program to sort the DataFrame

In this python pandas program, we will sort the DataFrame in ascending order for a column using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Sort the DataFrame first by ‘name’ in ascending order using df.sort_values(by=[‘Name’], ascending=[True]).
  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]}
df = pd.DataFrame(d)
print("Original Series: ")
print(df)
new = df.sort_values(by=['Name'], ascending=[True])
print("After sorting: ")
print(new)
				
			

Output :

				
					Original Series: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
After sorting: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
3       4  Klaus   33
2       3  Peter   29
				
			

add a new row in the DataFrame

calculate the mean of age column

Python Pandas program to add a new row in the DataFrame

In this python pandas program, we will add a new row in 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. Create a new DataFrame containing the new row.
  4. Now add that row to the original DataFrame using df.append(df1, ignore_index=True).
  5. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print("Original Series: ")
print(df)
df1 = {'Sr.no.':5,'Name':'Jason','Age':28}
df = df.append(df1, ignore_index=True)
print("New Series: ")
print(df)
				
			

Output :

				
					Original Series: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
New Series: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
4       5  Jason   28
				
			

calculate the sum of age column

sort the DataFrame first by ‘name’ in ascending order