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

Program to find the mean of the data of a given series

In this python pandas program,we will find the mean of the data of a 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 mean of the given series using df.mean().
  4. Print the output.
				
					import pandas as pd
df = pf.Series([5,4,8,6])
print(df)
print("Mean of the given series: ",df.mean())
				
			

Output :

				
					0    5
1    4
2    8
3    6
dtype: int64
Mean of the given series:  5.75
				
			

change the order of the index of a given series

find the standard deviation of the  given Series

Change the order of the index of a given series

In this python pandas program, we will change the order of the index of a given series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Change the order of the index of a given series using df.reindex(index=[2,3,0,1,4,5]).
  4. It will arrange the series in the above index order.
  5. Print the output.
				
					import pandas as pd
df = pd.Series(data = ['sqa','tools','learning','python','is','fun'],
              index = [0,1,2,3,4,5])
print("Original Data Series:")
print(df)
df = df.reindex(index = [2,3,0,1,4,5])
print("Data Series after changing the order of index:")
print(df)
				
			

Output :

				
					Original Data Series:
0         sqa
1       tools
2    learning
3      python
4          is
5         fun
dtype: object
Data Series after changing the order of index:
2    learning
3      python
0         sqa
1       tools
4          is
5         fun
dtype: object
				
			

create a subset of a given series based on value and condition

find the mean of the data of a given series

Create a subset of a given series using Pandas

In this python pandas program, we will create a subset of a given series using Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Now create a subset of a given series using logic in this case, df[df < 40].
  4. Print the output.
				
					import pandas as pd
df = pd.Series([10,25,69,74,33,54,21])
print(df)
print("Subset of the above Data Series:")
new = df[df < 40]
print(new)
				
			

Output :

				
					0    10
1    25
2    69
3    74
4    33
5    54
6    21
dtype: int64
Subset of the above Data Series:
0    10
1    25
4    33
6    21
dtype: int64
				
			

add some data to an existing series

change the order of the index of a given series

Program to add some data to an existing series

In this python pandas program, we will add some data to an existing series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Now add some data to the series using df.append(pd.Series([data])).
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,27.9,'sqa',33.33,'tools'])
print("Original Data Series:")
print(df)
print("Data Series after adding some data:")
new = df.append(pd.Series(['python',100]))
print(new)
				
			

Output :

				
					Original Data Series:
0       54
1     27.9
2      sqa
3    33.33
4    tools
dtype: object
Data Series after adding some data:
0        54
1      27.9
2       sqa
3     33.33
4     tools
0    python
1       100
dtype: object
				
			

sort a given Series

create a subset of a given series based on value and condition