Pandas

Series in Pandas
Creating a series
#importing Libraries
import pandas as pd
import numpy as np
#Initializing a numpy array
data = np.array(['O','S','S','A'])
#Converting a numpy array to series without adding index
S1 = pd.Series(data)
#Converting a numpy array to series with index
S2 = pd.Series(data,index=[100,101,102,103])
print(S1)
print(S2)
Output


my_series = np.random.rand(5,10)
my_series2 = pd.Series(my_series)
#generates error as data must be 1-dimensional to be a series
Create list from dictionary
#Declaring a dictionary
data = {'X':0,'Y':1,'Z':2}
$Converting a dictionary to series
series1 = pd.Series(data)
print(series1)

#With index
series2 = pd.Series(data,index=['Y','Z','W','X'])
print(series2)

Creating Series using a Scalar
Series = pd.Series(7,index=[0,1,2,3,4])
print(Series)

Accessing a Data in Series
Se = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print('The Series')
print(Se)
print('\nRetrive the first element of series')
print(Se[0])
print('\nRetrive the first three elements of series')
print(Se[:3])
print('\nRetrive the last three elements of series')
print(Se[-3:])
print('\nRetrive a single element by its index')
print(Se['a'])
print('\nRetrive the multiple element by their index')
print(Se[['a','c','e']])

Exploring and Analyzing a Series
Se = pd.Series([5,55,66,12,1])
print("Series mean value: ",Se.mean())
print("Series max value: ",Se.max())
print("Series min value: ",Se.min())
print("Series standard deviation: ",Se.std())
print('\n')
print(Se.describe())

ponru
0