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

Calculate the number of characters in each word in a series using Pandas

In this python pandas program, we will calculate the number of characters in each word in a series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Calculate the number of characters in each word in a series using df.map(lambda x: len(x)).
  4. Pass the word of the series to the lambda function using df.map().
  5. Use len() to calculate the number of characters in each word.
  6. Print the output.
				
					import pandas as pf
df = pd.Series(['virat','rohit','pant','shikhar'])
print("Original Series:")
print(df)
result = df.map(lambda x: len(x))
print("Number of characters in each word of series:")
print(result)
				
			

Output :

				
					Original Series:
0      virat
1      rohit
2       pant
3    shikhar
dtype: object
Number of characters in each word of series:
0    5
1    5
2    4
3    7
dtype: int64
				
			

convert the first and last character of each word to upper case in each word of a given series

convert a series of date strings to a time-series

Convert the first and last character to upper case using Pandas

In this python pandas program, we will convert the first and last character to upper case using Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Convert the first and last character to upper case using df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper()).
  4. Pass each word of the series to the lambda function using df.map().
  5. Now using indexing and upper() convert the first and last character to upper case.
  6. Print the output.
				
					import pandas as pd
df = pd.Series(['sqatools','python','data','science'])
print("Original Series:")
print(df)
result = df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper())
print("First and last character of each word to upper case:")
print(result)
				
			

Output :

				
					Original Series:
0    sqatools
1      python
2        data
3     science
dtype: object
First and last character of each word to upper case:
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: object
				
			

extract items at given positions of a series

calculate the number of characters in each word in a series