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

Program to sort a given Series using Pandas

In this python pandas program, we will sort 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 sort the given series using pd.Series(series_name).sort_values().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([55,23,10,87])
print("Original Data Series:")
print(df)
new = pd.Series(df).sort_values()
print(new)
				
			

Output :

				
					Original Data Series:
0    55
1    23
2    10
3    87
dtype: int64
2    10
1    23
0    55
3    87
dtype: int64
				
			

convert a series of lists into one series

add some data to an existing series

Program to convert a series of lists into one series

In this python pandas program, we will convert a series of lists into one series using Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Take a series of lists as input.
  3. Now convert a series of lists into one series using pd.Series().
  4. Print the output.
				
					import pandas as pd
a = [['Sqa','tool'],['Learning','python'],['Is','fun']]
result = pd.Series(a)
print(result)
				
			

Output :

				
					0           [Sqa, tool]
1    [Learning, python]
2             [Is, fun]
dtype: object
				
			

convert a given Series to an array

sort a given Series

Program to convert a given Series to an array

In this python pandas program, we will convert a given series to an array using NumPy and Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import NumPy library as np.
  3. Create a series using pd.Series().
  4. Now convert that series to an array using np.array(series_name.values.tolist())..
				
					import numpy as np
import pandas as pd
df1 = pd.Series([54,27.90,'sqa',33.33,'tools'])
print("Original Data Series:")
print(df1)
print("Series to an array")
result = np.array(df1.values.tolist())
print (result)
				
			

Output :

				
					Original Data Series:
0       54
1     27.9
2      sqa
3    33.33
4    tools
dtype: object
Series to an array
['54' '27.9' 'sqa' '33.33' 'tools']
				
			

change the data type of given a column or a Series

convert a series of lists into one series

Change the data type of given a column using Pandas

In this python pandas program, we will change the data type of given a column using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Change the data-type of the series from object to float using pd.to_numeric(series name, errors=’coerce’).
  4. It will convert all the non-string elements to float and if there is a string element it will show NaN instead.
  5. Print the output
				
					import pandas as pd
df1 = pd.Series([54,27.90,'sqa',33.33,'tools'])
print("Original Data Series:")
print(df1)
print("Change the said data type to numeric:")
df2 = pd.to_numeric(df1, errors='coerce')
print(df2)
				
			

Output :

				
					Original Data Series:
0       54
1     27.9
2      sqa
3    33.33
4    tools
dtype: object
Change the said data type to numeric:
0    54.00
1    27.90
2      NaN
3    33.33
4      NaN
dtype: float64
				
			

convert a NumPy array to a Pandas series

convert a given Series to an array

Program to convert a NumPy array to a Pandas series.

In this python pandas program, we will convert a NumPy array to a Pandas series.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import NumPy library as np.
  3. Create a NumPy array using np.array().
  4. Now convert that array to a Pandas series using pd.Series().
  5. Print the output.
				
					import numpy as np
import pandas as pd
array = np.array([5,3,8,9,0])
result = pd.Series(array)
print(result)
				
			

Output :

				
					0    5
1    3
2    8
3    9
4    0
dtype: int32
				
			

convert a dictionary to a series

change the data type of given a column or a Series

Program to convert a dictionary to a Pandas series.

In this python pandas program, we will convert a dictionary to a Pandas series.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dictionary.
  3. Now convert that dictionary to a series using pd.Series().
  4. Print the output.
				
					import pandas as pd
D = {'name':'Virat','sport':'cricket','age':32}
result = pd.Series(D)
print(result)
				
			

Output :

				
					name       Virat
sport    cricket
age           32
dtype: object
				
			

check whether elements in the series are greater than other series

convert a NumPy array to a Pandas series