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

Python pandas program to print the first n rows of a Dataframe

In this python pandas program, we will print the first n rows of a Dataframe using pandas library.

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 first n rows of a Dataframe using df.head(n).
				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
print("First n rows: \n",df.head(2))
				
			

Output :

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

convert a dictionary into DataFrame

print the last n rows of a DataFrame

Python pandas program to convert a dictionary into DataFrame

In this python pandas program, we will convert a dictionary into DataFrame using pandas library.

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 output.
				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
				
			

Output :

				
					0   marks1  marks2
0      34      36
1      20      22
2      32      10
3      30      44
				
			

find the index of the first occurrence of the smallest and largest value of a series

print the first n rows of a Dataframe

Find the index of the smallest and largest value using Pandas

In this python pandas program, we will find the index of the smallest and largest value using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the index of the smallest and largest values using df.idxmin() and df.idxmax().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,25,38,87,67])
print(df)
print("Index of the first smallest and largest value of the series:")
print(df.idxmin())
print(df.idxmax())
				
			

Output :

				
					0    54
1    25
2    38
3    87
4    67
dtype: int64
Index of the first smallest and largest value of the series:
1
3
				
			

convert a dictionary into DataFrame

Convert a series of date strings to a time-series using Pandas

In this python pandas program, we will convert a series of date strings to a time-series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series of date strings using pd.Series().
  3. Convert a series of date strings to a time-series using pd.to_datetime(df).
  4. Print the output.
				
					import pandas as pd
df = pd.Series(['2 Feb 2020','5/11/2021','7-8-2022'])
print("Original Series:")
print(df)
print("Converting series of date strings to a timeseries:")
print(pd.to_datetime(df))
				
			

Output :

				
					Original Series:
0    2 Feb 2020
1     5/11/2021
2      7-8-2022
dtype: object
Converting series of date strings to a timeseries:
0   2020-02-02
1   2021-05-11
2   2022-07-08
dtype: datetime64[ns]
				
			

calculate the number of characters in each word in a series