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 select the missing rows

In this python pandas program, we will select the missing rows using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Select the missing rows in the age column using df[df[‘Age’].isnull()].
  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,np.nan,29,np.nan]}
df = pd.DataFrame(d)
print(df)
print("Rows where age is missing:")
print(df[df['Age'].isnull()])
				
			

Output :

				
					0   Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
Rows where age is missing:
   Sr.no.   Name  Age
1       2   John  NaN
3       4  Klaus  NaN
				
			

count the number of rows and columns in a DataFrame

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

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

Python Pandas program to select the rows where the age is greater than a number

In this python pandas program, we will select the rows where the age is greater than a number using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Select the rows where the age is greater than a number using df[df[‘Age’] > 29].
  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]}
df = pd.DataFrame(d)
print(df)
print("Rows where age is greater than 29")
print(df[df['Age'] > 29])
				
			

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 greater than 29
   Sr.no.   Name  Age
0       1   Alex   30
3       4  Klaus   33
				
			

print the selected rows from DataFrame

count the number of rows and columns in a DataFrame

Python Pandas program to print the selected rows from DataFrame

In this python pandas program, we will print the selected rows from DataFrame 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 selected rows from DataFrame using df.iloc[], it is the same as indexing.
  4. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3],'Name':['Alex','John','Peter'],'Age':[30,27,29]}
df = pd.DataFrame(d)
print("Original dataframe: \n",df)
print("Second row: \n",df.iloc[1,:])
				
			

Output :

				
					Original dataframe: 
    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
Second row: 
 Sr.no.       2
Name      John
Age         27
Name: 1, dtype: object
				
			

print the selected columns from DataFrame

select the rows where the age is greater than 29

Python pandas program to print the selected columns from DataFrame

In this python pandas program, we will print the selected columns from DataFrame 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 selected columns from DataFrame using df[[‘Name’,’Age’]].
  4. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3],'Name':['Alex','John','Peter'],'Age':[30,27,29]}
df = pd.DataFrame(d)
print("Original dataframe: \n",df)
print(df[['Name','Age']])
				
			

Output :

				
					Original dataframe: 
    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
    Name  Age
0   Alex   30
1   John   27
2  Peter   29
				
			

print the last n rows of a DataFrame

print the selected rows from DataFrame

Python Pandas program to print the last n rows of a DataFrame

In this python pandas program, we will print the last n rows of a DataFrame using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dictionary.
  3. Now convert that dictionary to  DataFrame using pd.DataFrame().
  4. Print the last n rows of a Dataframe using df.tail(n).
				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
print("Last n rows: \n",df.tail(2))
				
			

Output :

				
					0   marks1  marks2
0      34      36
1      20      22
2      32      10
3      30      44
Last n rows: 
    marks1  marks2
2      32      10
3      30      44
				
			

print the first n rows of a Dataframe

print the selected columns from DataFrame