Pandas is a library module designed in Python programming. It is specific for data structure with labeled data. Pandas provides two data structure types, Series and Data Frame. Series is a one-dimensional labeled data, and Data Frame is used for tabular data storage.
Series stores data values and their corresponding labels in two arrays: its value part and index part. To create a Series, function Series() from Pandas can be just applied. Following example show how to create a Series.
#import module Numpy and Pandas
import pandas as pd
import numpy as np
#create a Series, T, with input an array of 4 elements
T = pd.Series([32, 28, 19, 301])
T
#output
Out[139]:
0 32
1 28
2 19
3 301
dtype: int64
We can see that Pandas assign index from 0 by default to the Series. Next example shows creating a Series with manually assigning its index.
#create a Series, manually assigning its index
P = pd.Series([32, 28, 19, 301], index=['l', 'm', 'n', 'o'])
P
#output
Out[140]:
l 32
m 28
n 19
o 301
dtype: int64
#Series's values part
P.values
#output
array([ 32, 28, 19, 301], dtype=int64)
#Series's index part
P.index
#output
Index(['l', 'm', 'n', 'o'], dtype='object')
A Series’s internal elements can be returned with square brackets and the corresponding index.
#value of Series, associated with third index
P[2]
#output
19
#element of Series, associated with index 'o'
P['o']
#output
301
#elements of Series, associated with first to third indices
P[0:3]
#output, is another Series
l 32
m 28
n 19
dtype: int64
#elements of Series, associated with indices 'm' and 'o'
P[['m','o']]
#output, is another Series
m 28
o 301
dtype: int64
0 Comments