Series is one of the Data Structure of Python Pandas which stores data in Linear fashion. Series is
While creating series from numpy arrays, we firstly create a numpy array and then we pass the created
numpy array from Series funcion. If you guys are not familiar with arrays, so first you must be knowing
what it actually is.
Arrays are datatypes which stores similar type of data i.e. either int or
float or string/object in contiguous memory locations.
#importing required libraries
import pandas as pd
import numpy as np
#creating a numpy array of names
arr = np.array(['Aman', 'Raghav', 'Shubham', 'Rohit', 'Yash'])
ser = pd.Series(arr)
#printing the created series below
print(ser)
0 | Aman |
1 | Raghav |
2 | Shubham |
3 | Rohit |
4 | Yash |
dtype: | object |
Lists are basically the collections of items which can be either of same datatype or different or both
simultaneouly. List elements are enclosed by [] and are seperated by commas.
Eg. kirana = ['Sugar', 'Rice','Tea','Cashew',100,68.8]
#importing required libraries
import pandas as pd
#creating a list of kirana items😂🤣
kirana = ['Sugar','Rice','Tea','Oil','Cookies']
ser = pd.Series(kirana)
#printing the created series below
print(ser)
0 | Sugar |
1 | Rice |
2 | Tea |
3 | Oil |
4 | Cookies |
dtype: | object |
Dictionary is also a datatype of Python which stores data in the form of key and value pair. Every value that is element is associated with its corresponding key which can be used to access the value. In a dictionary, every pair is seperated with commas(,) and every key and its value is seperated by colon(:).
Example:
dict = key1:value1, key2:value2, key3:value3, ------
days = {1:'Sunday', 2:'Monday', 3:'Tuesday', 4:'Wednesday', 5:'Thursday', 6:'Friday', 7:'Saturday'}
Note:When we create series from dictionary, key becomes the index of the series and value becomes the data of the Series.
#importing required libraries
import pandas as pd
#creating a dictionary of days of week
days = {'Day1':'Sunday', 'Day2':'Monday', 'Day3':'Tuesday', 'Day4':'Wednesday', 'Day5':'Thursday', 'Day6':'Friday', 'Day7':'Saturday'}
ser = pd.Series(days)
#printing the created series below
print(ser)
Day1 | Sunday |
Day2 | Monday |
Day3 | Tuesday |
Day4 | Wednesday |
Day5 | Thursday |
Day6 | Friday |
Day7 | Saturday |
dtype: | object |
If we want to create the series such that all the data values are to be same so, we can use the method of scalar value. For this, we use a range function which gives the number of values in the resultant series.
#importing required libraries
import pandas as pd
#creating a series of scalar value
ser = pd.Series("Welcome to IPHM",range(0,5))
#printing the created series below
print(ser)
0 | Welcome to IPHM |
1 | Welcome to IPHM |
2 | Welcome to IPHM |
3 | Welcome to IPHM |
4 | Welcome to IPHM |
5 | Welcome to IPHM |
dtype: | object |