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

Python Pandas program to calculate the sum of a column

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

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Calculate the sum of the age column using df[‘Age’].sum().
  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'].sum()
				
			

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:  119
				
			

change the age of John to 24

add a new row in the DataFrame

Python Pandas program to change the observation in the DataFrame

In this python pandas program, we will change the observation 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. Change the age of John to 24 in the DataFrame using df[‘Age’].replace(27,24).
  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 24:")
df['Age'] = df['Age'].replace(27,24)
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 24:
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   24
2       3  Peter   29
3       4  Klaus   33
				
			

print the names who’s age is between 25-30 using Pandas

calculate the sum of age column

Python Pandas program to print the names

In this python pandas program we will print the names who’s age is between 25-30 using the Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Print the names who’s age is between 25-30 using df[df[‘Age’].between(25,30)].
  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("Rows where age is between 25 and 30 (inclusive):")
print(df[df['Age'].between(25,30)])
				
			

Output :

				
					0   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
Rows where age is between 25 and 30 (inclusive):
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
				
			

select the rows where age is missing

change the age of John to 24

Python Pandas program to count the number of rows and columns in a DataFrame

In this python pandas program, we will count the number of rows and columns 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. Count the number of rows and columns in a DataFrame using df.shape().
  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("No. of rows: ",df.shape[0])
print("No. of columns: ",df.shape[1])
				
			

Output :

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

select the rows where the age is greater than 29

select the rows where age is missing