Python Pandas Series
Python Pandas Series
Pandas is a Python package providing fast, flexible, and expressive data structures designed to
make working with 'relational' or 'labeled' data both easy and intuitive. It aims to be the
fundamental high-level building block for doing practical, real world data analysis in Python.
import pandas as pd
Now to the basic components of pandas.
DataFrames and Series are quite similar in that many operations that you can do
with one you can do with the other, such as filling in null values and calculating
the mean.
import pandas as pd
import numpy as np
Pandas version:
import pandas as pd
print(pd.__version__)
Key and Imports
Create DataSeries:
import pandas as pd
L=[2,4,6,8,10]
s = pd.Series([2, 4, 6, 8, 10])
s = pd.Series(L)
print(s)
Sample Output:
0 2
1 4
2 6
3 8
4 10
dtype: int64
Create Dataframe:
import pandas as pd
df = pd.DataFrame({'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':
[86,97,96,72,83]});
print(df)
Sample Output:
X Y Z
0 78 84 86
1 85 94 97
2 96 89 96
3 80 83 72
4 86 86 83
Create a Series in python – pandas
1
2
3
4
5
# Example Create an Empty Series
import pandas as pd
s = pd.Series()
print s
output:
1
2
3
4
5
6
7
# Example Create a series from array
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d','e','f'])
s = pd.Series(data)
print s
output:
0 a
1 b
2 c
3 d
4 e
5 f
dtype: object
Create a series from array with index:
This example depicts how to create a series in
python with index, Index starting from 1000 has
been added in the below example.
1
2
3
4
5
6
7
# Example Create a series from array with
specified index
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d','e','f'])
s=
pd.Series(data,index=[1000,1001,1002,1003,1004,1
005])
print s
output:
1000 a
1001 b
1002 c
1003 d
1004 e
1005 f
dtype: object
import pandas as pd
s = pd.Series(data,index=['b','c','d','a'])
print s
Index order is maintained and the missing element
is filled with NaN (Not a Number). So the output will
be
output:
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
Create a series from Scalar value
This example depicts how to create a series in
python from scalar value. If data is a scalar value,
an index must be provided. The value will be
repeated to match the length of index
import pandas as pd
import numpy as np
s = pd.Series(7, index=[0, 1, 2, 3])
print s
output:
0 7
1 7
2 7
3 7
dtype: int64
How to Access the elements of a Series in
python – pandas
# create a series
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d','e','f'])
s = pd.Series(data)
a
Access or Retrieve the first three elements in
the Series:
# create a series
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d','e','f'])
s = pd.Series(data)
0 a
1 b
2 c
dtype: object
3 d
4 e
5 f
dtype: object
# create a series
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d','e','f'])
s=pd.Series(data,index=[100,101,102,103,104,105])
print s[102]
output:
c
print s[[102,103,104]]
output:
102 c
103 d
104 e
dtype: object
http://www.datasciencemadesimple.com/access-elements-series-python-pandas/
Series is a one-dimensional labeled array capable of holding data of any type (integer,
string, float, python objects, etc.). The axis labels are collectively called index.
pandas.Series
A pandas Series can be created using the following constructor −
pandas.Series( data, index, dtype, copy)
The parameters of the constructor are as follows −
1
data
data takes various forms like ndarray, list, constants
2
index
Index values must be unique and hashable, same length as data. Default np.arrange(n) if no index is
passed.
3
dtype
dtype is for data type. If None, data type will be inferred
4
copy
Copy data. Default False
Array
Dict
Scalar value or constant
Example
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print s
Its output is as follows −
Series([], dtype: float64)
Example 1
Its output is as follows −
0 a
1 b
2 c
3 d
dtype: object
We did not pass any index, so by default, it assigned the indexes ranging from 0
to len(data)-1, i.e., 0 to 3.
Example 2
Its output is as follows −
100 a
101 b
102 c
103 d
dtype: object
We passed the index values here. Now we can see the customized indexed values in the
output.
Example 1
Its output is as follows −
a 0.0
b 1.0
c 2.0
dtype: float64
Observe − Dictionary keys are used to construct index.
Example 2
Its output is as follows −
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
Observe − Index order is persisted and the missing element is filled with NaN (Not a
Number).
Its output is as follows −
0 5
1 5
2 5
3 5
dtype: int64
Retrieve the first element. As we already know, the counting starts from zero for the array,
which means the first element is stored at zeroth position and so on.
Live Demo
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
Its output is as follows −
1
Example 2
Retrieve the first three elements in the Series. If a : is inserted in front of it, all items from
that index onwards will be extracted. If two parameters (with : between them) is used,
items between the two indexes (not including the stop index)
Live Demo
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
Its output is as follows −
a 1
b 2
c 3
dtype: int64
Example 3
Its output is as follows −
c 3
d 4
e 5
dtype: int64
Example 1
Its output is as follows −
1
Example 2
Its output is as follows −
a 1
c 3
d 4
dtype: int64
Example 3
Its output is as follows −
…
KeyError: 'f'
Python Programs
>>>
0 101
1 102
2 103
3 104
4 105
dtype: int64
S1=pd.Series([101,102,103,104,105],index=['A1','B1','C1','D1','E1'])
print(S1)
>>>
A1 101
B1 102
C1 103
D1 104
E1 105
dtype: int64
print(S2)
>>>
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
dtype: int64
>>>
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
5 5.0
6 6.0
7 7.0
8 8.0
9 9.0
dtype: float32
print(S3)
print(S3.index)
print(S3.values)
print(S3.dtype)
print(S3.shape)
print(S3.nbytes)
print(S3.ndim)
print(S3.itemsize)
print(S3.size)
print(S3.hasnans)
>>>
Anil 20.0
BN NaN
BM NaN
Ankit 45.0
Ram 67.0
Vishal 89.0
Ankita 54.0
Lokesh 45.0
Venkat 23.0
dtype: float64
dtype='object')
[20. nan nan 45. 67. 89. 54. 45. 23.]
float64
(9,)
72
1
8
9
True
#6.Accessing elements of Series
print(S3)
Anil 20.0
BN NaN
BM NaN
Ankit 45.0
Ram 67.0
Vishal 89.0
Ankita 54.0
Lokesh 45.0
Venkat 23.0
dtype: float64
print(S3[6])
>>>54.0
print(S3[:2])
>>>
Anil 20.0
BN NaN
dtype: float64
print(S3[1:4])
>>>
BN NaN
BM NaN
Ankit 45.0
dtype: float64
#7.Series with two different Lists
dayno=[1,2,3,4,5,6,7]
dayname=["Monday","Tuesday","Wednesday","Thursday","Friday",
"Saturday","Sunday"]
ser_week=pd.Series(dayname,index=dayno)
print(ser_week)
>>>
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
dtype: object
#import numpy as np
S1=pd.Series([101,102,103,104,np.NaN,90.7])
print(S1)
>>>
0 101.0
1 102.0
2 103.0
3 104.0
4 NaN
5 90.7
dtype: float64
D1={'1':'Monday','2':'Tuesday','3':'Wednesday','4':'Thursday',
'5':'Friday','6':'Saturday','7':'Sunday'}
print(D1)
S5=pd.Series(D1)
print(S5)
>>>
{'1': 'Monday', '2': 'Tuesday', '3': 'Wednesday', '4': 'Thursday',
'5': 'Friday', '6': 'Saturday', '7': 'Sunday'}
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
dtype: object
>>>
a 90.7
b 90.7
c 90.7
d 90.7
e 90.7
f 90.7
g 90.7
dtype: float64
S7=pd.Series(90)
print(S7)
>>>
0 90
dtype: int64
S8=pd.Series(90,index=[1])
print(S8)
>>>
1 90
dtype: int64
>>>
0 95
1 95
2 95
3 95
4 95
dtype: int64
#12. iloc() Method
S8=pd.Series([1,2,3,4,5,6,7],index=['a','b','c','d','e','f','g'])
print(S8.iloc[1:5])
>>>
b 2
c 3
d 4
e 5
dtype: int64
print(S8.loc['b':'e'])
>>>
b 2
c 3
d 4
e 5
dtype: int64
#14.Extract those values of series for specified
index positions - take() Method
dayno=[91,92,93,94,95,96,97]
dayname=["Monday","Tuesday","Wednesday","Thursday","Friday",
"Saturday","Sunday"]
ser_week=pd.Series(dayname,index=dayno)
print(ser_week)
>>>
91 Monday
92 Tuesday
93 Wednesday
94 Thursday
95 Friday
96 Saturday
97 Sunday
dtype: object
pos=[0,2,5]
print(ser_week.take(pos))
>>>
91 Monday
93 Wednesday
96 Saturday
dtype: object
print(ser_week[91])
>>>
Monday
#15.Stack 2 Series horizontally
ss1=pd.Series([1,2,3,4,5],index=[11,12,13,14,15])
ss2=pd.Series(['a','b','c','d','e'])
print(ss1.append(ss2))
>>>
11 1
12 2
13 3
14 4
15 5
0 a
1 b
2 c
3 d
4 e
dtype: object
print(ss1)
>>>
11 1
12 2
13 3
14 4
15 5
dtype: int64
print(ss2)
>>>
0 a
1 b
2 c
3 d
4 e
dtype: object
ss3=ss1.append(ss2)
print(ss3)
11 1
12 2
13 3
14 4
15 5
0 a
1 b
2 c
3 d
4 e
dtype: object
head() function with no arguments gets the first five rows of data from the data
series .
tail() function with no arguments gets the last five rows of data from the data
series.
import pandas as pd
S8=pd.Series([1,2,3,4,5,6,7],index=['a','b','c','d','e','f','g'])
print(S8.head())
print(S8.tail())
print(S8.head(7))
print(S8.tail(6))
>>>
RESTART: C:/Users/naman/AppData/Local/Programs/Python/Python37-
32/panda-series.py
0 101
1 102
2 103
3 104
4 105
dtype: int64
A1 101
B1 102
C1 103
D1 104
E1 105
dtype: int64
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
dtype: int64
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
5 5.0
6 6.0
7 7.0
8 8.0
9 9.0
dtype: float32
Anil 20.0
BN NaN
BM NaN
Ankit 45.0
Ram 67.0
Vishal 89.0
Ankita 54.0
Lokesh 45.0
Venkat 23.0
dtype: float64
Index(['Anil', 'BN', 'BM', 'Ankit', 'Ram', 'Vishal', 'Ankita',
'Lokesh',
'Venkat'],
dtype='object')
[20. nan nan 45. 67. 89. 54. 45. 23.]
float64
(9,)
72
1
9
True
Anil 20.0
BN NaN
BM NaN
Ankit 45.0
Ram 67.0
Vishal 89.0
Ankita 54.0
Lokesh 45.0
Venkat 23.0
dtype: float64
54.0
Anil 20.0
BN NaN
dtype: float64
BN NaN
BM NaN
Ankit 45.0
dtype: float64
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
dtype: object
0 101.0
1 102.0
2 103.0
3 104.0
4 NaN
5 90.7
dtype: float64
{'1': 'Monday', '2': 'Tuesday', '3': 'Wednesday', '4': 'Thursday',
'5': 'Friday', '6': 'Saturday', '7': 'Sunday'}
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
dtype: object
a 90.7
b 90.7
c 90.7
d 90.7
e 90.7
f 90.7
g 90.7
dtype: float64
0 90
dtype: int64
1 90
dtype: int64
0 95
1 95
2 95
3 95
4 95
dtype: int64
b 2
c 3
d 4
e 5
dtype: int64
b 2
c 3
d 4
e 5
dtype: int64
91 Monday
92 Tuesday
93 Wednesday
94 Thursday
95 Friday
96 Saturday
97 Sunday
dtype: object
91 Monday
93 Wednesday
96 Saturday
dtype: object
Monday
11 1
12 2
13 3
14 4
15 5
0 a
1 b
2 c
3 d
4 e
dtype: object
11 1
12 2
13 3
14 4
15 5
dtype: int64
0 a
1 b
2 c
3 d
4 e
dtype: object
11 1
12 2
13 3
14 4
15 5
0 a
1 b
2 c
3 d
4 e
dtype: object
>>>