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

Check whether elements in the series are equal or not

In this python pandas program, we will check whether elements in the series are equal or not.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two series using pd.Series().
  3. Check whether elements in the series are equal or not using ” == “.
  4. Print the output.
				
					import pandas as pd
df1 = pd.Series([1,6,9,5])
df2 = pd.Series([5,2,4,5])
print("Equals:")
print(df1 == df2)
				
			

Output :

				
					Equals:
0    False
1    False
2    False
3     True
dtype: bool
				
			

divide two series

check whether elements in the series are greater than other series

Program to divide two series

In this python pandas program, we will divide two series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two series using pd.Series().
  3. Now divide the two series using the ” / ” operator.
  4. Print the output.
				
					import pandas as pd
df1 = pd.Series([1,6,9,5])
df2 = pd.Series([5,2,4,5])
print(df1/df2)
				
			

Output :

				
					0    0.20
1    3.00
2    2.25
3    1.00
dtype: float64
				
			

multiply two series

check whether elements in the series are equal or not

Program to multiply two series

In this python pandas program, we will multiply two series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two series using pd.Series().
  3. Now multiply the two series using the ” * ” operator.
  4. Print the output.
				
					import pandas as pd
df1 = pd.Series([1,6,9,5])
df2 = pd.Series([5,2,4,5])
print(df1*df2)
				
			

Output :

				
					0     5
1    12
2    36
3    25
dtype: int64
				
			

subtract two series

divide two series

Program to subtract two series

In this python pandas program, we will subtract two series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two series using pd.Series().
  3. Now subtract the two series using the ” ” operator.
  4. Print the output.
				
					import pandas as pd
df1 = pd.Series([1,6,9,5])
df2 = pd.Series([5,2,4,5])
print(df1-df2)
				
			

Output :

				
					0   -4
1    4
2    5
3    0
dtype: int64
				
			

add two series

multiply two series