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

Extract items at given positions of a series using Pandas

In this python pandas program, we will extract items at given positions of a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Extract items at given positions of a series using df.take(positions) where positions are the index no of values that we want to extract.
  4. Print the output.
				
					import pandas as pd
df = pd.Series([3,0,3,2,2,0,3,3,2])
positions = [1,4,7]
print("Original Series:")
print(df)
result = df.take(positions)
print("Extract items at given positions of the said series:")
print(result)
				
			

Output :

				
					Original Series:
0    3
1    0
2    3
3    2
4    2
5    0
6    3
7    3
8    2
dtype: int64
Extract items at given positions of the said series:
1    0
4    2
7    3
dtype: int64

				
			

calculate the frequency of each value of a series

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

Calculate the frequency of each value of a series using Pandas

In this python pandas program, we will calculate the frequency of each value of a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Calculate the frequency of each value of a series using df.value_counts().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([3,0,3,2,2,0,3,3,2])
print(df)
result = df.value_counts()
print(result)
				
			

Output :

				
					0    3
1    0
2    3
3    2
4    2
5    0
6    3
7    3
8    2
dtype: int64
3    4
2    3
0    2
dtype: int64
				
			

calculate the minimum value from a series

extract items at given positions of a series

Calculate the minimum value from a series using Pandas

In this python pandas program, we will calculate the minimum value from a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the minimum value of the series using df.min().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,38,67,87])
print(df)
print("Minumum value: ",df.min())
				
			

Output :

				
					0    54
1    38
2    67
3    87
dtype: int64
Minumum value:  38
				
			

calculate the maximum value from a series

calculate the frequency of each value of a series

Calculate the maximum value from a series using Pandas

In this python pandas program, we will calculate the maximum value from a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the maximum value of the series using df.max().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,38,67,87])
print(df)
print("Maximum value: ",df.max())
				
			

Output :

				
					0    54
1    38
2    67
3    87
dtype: int64
Maximum value:  87
				
			

get the items of a series not present in another series

calculate the minimum value from a series

Get the items of a series not present in another series using Pandas

In this python pandas program, we will get the items of a series not present in another series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two series using pd.Series().
  3. Get the items of a series not present in another series using df1[~df1.isin(df2)].
  4. Print the output.
				
					import pandas as pd 
df1 = pd.Series([4,7,3,10])
df2 = pd.Series([9,4,3])
print("First series: \n",df1)
print("Second series: \n",df2)
result = df1[~df1.isin(df2)]
print(result)
				
			

Output :

				
					First series: 
 0     4
1     7
2     3
3    10
dtype: int64
Second series: 
 0    9
1    4
2    3
dtype: int64
1     7
3    10
dtype: int64
				
			

find the standard deviation of the  given Series

calculate the maximum value from a series

Program to find the standard deviation of the given series

In this python pandas program, we will find the standard deviation of the given series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the standard deviation of the given series using df.std().
  4. Print the output.
				
					import pandas as pd
df = pf.Series([2,4,6,8,10])
print(df)
print("Standard deviation of the given series: ",df.std())
				
			

Output :

				
					0     2
1     4
2     6
3     8
4    10
dtype: int64
Standard deviation of the given series:  3.1622776601683795
				
			

find the mean of the data of a given series

get the items of a series not present in another series