0% found this document useful (0 votes)
92 views

Revision Point - Series

Pandas is a popular Python library used for data analysis and manipulation of structured data. It allows users to easily work with one-dimensional Series and two-dimensional DataFrame data structures. A Series is a one-dimensional array-like structure that can hold any data type (integers, strings, lists etc.), along with an associated array of indexes. A Series can be created from many types of data like lists, arrays, dictionaries etc. using the Series() function in Pandas.

Uploaded by

zahir khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Revision Point - Series

Pandas is a popular Python library used for data analysis and manipulation of structured data. It allows users to easily work with one-dimensional Series and two-dimensional DataFrame data structures. A Series is a one-dimensional array-like structure that can hold any data type (integers, strings, lists etc.), along with an associated array of indexes. A Series can be created from many types of data like lists, arrays, dictionaries etc. using the Series() function in Pandas.

Uploaded by

zahir khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Pandas Series & Data Frame

Python Pandas Series & DataFrame


Creation of Series:
Python Pandas: A series of object can be created by using many
Pandas is most popular library. It provides ways. Like
various functions related to Scientific Data 1. Creation of empty series by using Series()
analysis, like 2. Creation of non- empty series with Series()
Pandas is Python's library for data analysis.
Pandas has derived its name from PANel DAta 1. Creation of empty series:
System. Pandas developed by Wes McKinney Syntax:
It can read and write different data formats like Series_object = pandas.Series()
int, float, double # S is capital in Series()
It can calculate data that is organized in row Example:
and columns. import pandas
It can select sub set of data values and merge Ser_obj1 = pandas.Series()
two data sets. # It will create an empty series of float type.
It can support reshape of data values.
It can support visualization library matplotlib. 2. Creation of Non empty series
Syntax:
Data Structure: Series_object = pandas.Series(data, index=idx)
Pandas Data Structure is a way to store & Where data is array of actual data value of series.
organize data values in a specific manner so that Index is any valid numpy datatype. Index can be
various specific functions can be applied on any type of following.
them. Examples- array, stack, queue, linked list, A Python sequence
series, DataFrame etc. An nd array
A Python dictionary
A scalar value
Vs Example:
Property Series DataFrame
Ser_obj2 = pandas.Series([1,3,5])
Dimensions One-Dimensional Two-Dimensional Output:
Types of Homogenous Heterogeneous
0 1
data (In Series, all data (In DataFrame,
1 3
values should be of data values may
2 5
same type) be of different
type)
Ser_obj3 = pandas.Series([1.5,3.5,5.5])
Value Yes, Mutable Yes, Mutable
Output:
Mutable 0 1.5
Size Size is Immutable. Size is Mutable. 1 3.5
Mutable Once the size of series Once the size of 2 5.5
created, it cannot be DataFrame Creation of Series for various Objects:
changed. created, it can be Series of List (Integer values)
(If add/delete changed. import pandas as pd Series of Object-1
element, then new S1=pd.Series([2,4,6]) 0 2
series object will be p - 1 4
created.) print(S1) 2 6
Series of Tuple (Integer values)
import pandas as pd Series of Object-2
A Series is a Pandas Data Structure that S2=pd.Series((20,40,60)) 0 20
represent 1 Dimensional array of indexed data. p - 1 40
The series structure contains two parts. It print(S2) 2 60
requires to import pandas and numpy package. Series of List (Character values)
1. An array of actual data values import pandas as pd Series of Object-3
2. An associated array of indexes (Used to access 0 K
data values) p - 1 V
print(S3) 2 S
Page 1
5|K V S R E G I O N A L O F F I C E , J A I P U R | S U B J E C T - I N F O R M A T I C S P R A C T I C E S ( T E R M - I S E S S I O N 2 0 2 1 - 2 2 )
Series of List (string value) p -12) 2 5.5
import pandas as pd Series of Object-4 print(S12)
0 KVS JJN
p - Series of None values
print(S4) import pandas as pd Series of Object-
Series of List (String values) import numpy as np 13
import pandas as pd Series of Object-5 S13=pd.Series([9.5,np.None, 0 9.5
0 KVS 5.5]) 1 None
p - 1 JJN p -13) 2 5.5
print(S5) print(S13)
Series of array using arange() Series by using for loop
import pandas as pd Series of Object-
import pandas as pd Series of Object-6 import numpy as np 14
import numpy as np 0 3.0 A 1
nd1=np.arange(3, 13, 3.5) 1 6.5 S15=pd.Series(range(1,15,3), B 4
S6=pd.Series(nd1) 2 10.0 index=ind) C 7
p Object- p -14) D 10
print(S6) print(S14) E 13
Series of array using linspace() Series() Special examples
import pandas as pd Series of Object-
import pandas as pd Series of Object-7 import numpy as np 15
import numpy as np 0 24.0 arr=np.array([31,28,31,30]) Jan 31.0
nd2=np.linspace(24, 64, 5) 1 34.0 day=['Jan','Feb','Mar','Apr'] Feb 28.0
S7=pd.Series(nd2) 2 44.0 S15=pd.Series(data=arr,inde Mar 31.0
p - 3 54.0 x=day, dtype=np.float64) Apr 30.0
print(S7) 4 64.0 print("Series of Object-15")
Series of dictionary print(S15)
import pandas as pd Series of Object-8 Series() Special examples
import numpy as np Feb 28 import pandas as pd Series of Object-
Jan 31 import numpy as np 16
Mar 31 a=np.arange(9,13) 9 81
p - S16=pd.Series(index=a, 10 100
print(S8) data=a**2) 11 121
Series using range() print("Series of Object-16") 12 144
import pandas as pd Series of Object-9 print(S16)
S9=pd.Series(10, 0 10 Series() Special examples
index=range(0,3)) 1 10 import pandas as pd Series of Object-
p - 2 10 import numpy as np 17
print(S9) lst=[9,10,11] 0 9
Series of scalar values using user defined S17=pd.Series(data=lst*2) 1 10
index print("Series of Object-17") 2 11
import pandas as pd Series of Object- print(S17) 3 9
S11=pd.Series(20, 11 4 10
Raj 20 5 11
p -11) PB 20 Attributes of Series Object
print(S11) HR 20 Attribute Description
Series of NaN (Not a Number) values Series_object. It show the indexes of series
import pandas as pd Series of Object- index object
import numpy as np 12 Series_object. It show the nd-array values of
S12=pd.Series([9.5,np.NaN,5. 0 9.5 values series object
5]) 1 NaN

Page2
6|K V S
REGIONAL OFFICE, JAIPUR |SUBJECT-INFORMATICS PRACTICES (TERM -I SESSION
2021-22)
Series_object. It show the data types of data Apr 30
dtype values of series object dtype: int64
Series_object. It show tuple of shape print( Sr_Obj['Feb']) 28
shape underlying data of series object print(Sr_Obj['Apr']) 30
Series_object. It show the number of bytes of Accessing Slice of Series
nbytes underlying data of series object Slicing takes place position wise (built in Index)
Series_object. It show the number of and not the index wise in a series object.
ndim dimensions of underlying data Syntax: Series_Object[Start: End: Step]
of series object Where,
Series_object. It show the number elements in Start is Lower Limit (default is 0)
size series object End is Upper Limit
Series_object. It show the size of data type of Step is updation (default is 1)
itemsize underlying data of series object Note: slicing may be ve also
Series_object. It show True if there is NaN / print(Sr_Obj[1:3:1]) Feb 28
hasnans None value in Series, otherwise Mar 31
returns False. print(Sr_Obj[-1:-3:-1]) Apr 30
Series_object. It returns True if series is Mar 31
empty empty, otherwise returns False. print(Sr_Obj[1::]) Feb 28
Mar 31
Example with Attribute Output Apr 30
# Example of Series print(Sr_Obj[::1]) Jan 31
import numpy as np Feb 28
import pandas as pd Mar 31
Apr 30
Val=[31,28,31,30] print(Sr_Obj[::-1]) Apr 30
Sr_Obj=pd.Series(data=Val, index=Ind) Mar 31
Feb 28
print(Sr_Obj.index) Index(['Jan', 'Feb', Jan 31
'Mar', 'Apr'], Modifying Elements of of Series
dtype='object')
print(Sr_Obj.values) [31 28 31 30] Syntax: Series_Object[index / slice]= new value
print(Sr_Obj.dtype) int64 Sr_Obj[1]=29 Jan 31
print(Sr_Obj.itemsize) 8 print(Sr_Obj) Feb 29
print(Sr_Obj.size) 4 Mar 31
print(Sr_Obj.ndim) 1 Apr 30
print(Sr_Obj.empty) False Sr_Obj[:-3:-1]=31 Change 31 in Last
print(Sr_Obj.hasnans) False print(Sr_Obj) 2 place
print(Sr_Obj.nbytes) 32 Jan 31
print(Sr_Obj.shape) (4,) Feb 29
Mar 31
Accessing individual element of Series Apr 31
print("Add New element Add New element
Syntax: Series_Object[Valid index] 100") 100
import numpy as np Sr_Obj['May']=100 Jan 31
import pandas as pd print(Sr_Obj) Feb 29
Mar 31
Val=[31,28,31,30] Apr 31
Sr_Obj=pd.Series(data=Val, index=Ind) May 100
# print Whole series Jan 31 print("Delete Last index") Delete Last index
Feb 28 del Sr_Obj['May'] Jan 31
print(Sr_Obj) Mar 31 print(Sr_Obj) Feb 29

Page 3
7|K V S R E G I O N A L O F F I C E , J A I P U R | S U B J E C T - I N F O R M A T I C S P R A C T I C E S ( T E R M - I S E S S I O N
2021-22)
Mar 31 print(Sr_Obj.tail(6)) Mar 31
Apr 31 Apr 30
print("Rename Index") Rename Index May 31
Sr_Obj.index=['J','F','M','A'] J 31 Jun 30
print(Sr_Obj) F 29 Jul 31
M 31 Vector operations on Series Object
A 31 Similar to nd-array, the vector operations can be
applied on series object also. Scalar operation
mean, one operation can be applied to each
head( ) and tail( ) element of series object at a time.
import pandas as pd
head( ) returns first n rows and tail( ) returns import numpy as np
last n rows from series.
If n is not given then by default it will return 5 data=[10,20,30,40])
rows.
Sytax: print("Add 5 in each element of Add 5 in each
Series_Object.head([n]) Sr_Obj") element of
Series_Object.tail([n]) print(Sr_Obj+5) Sr_Obj
import numpy as np A 15
import pandas as pd B 25
Ind=['Jan','Feb','Mar','Apr','May','Jun','Jul'] C 35
Val=[31,28,31,30,31,30,31] D 45
Sr_Obj=pd.Series(data=Val, index=Ind) print("Multiply by 5 in each Add 5 in each
element of Sr_Obj") element of
print("Display First 2 Display First 2 Rows print(Sr_Obj*5) Sr_Obj
Rows") Jan 31 A 50
print(Sr_Obj.head(2)) Feb 28 B 100
print("Display First 5 Display First 5 Rows C 150
Rows") Jan 31 D 200
print(Sr_Obj.head()) Feb 28 print("Divide 5 in each element Add 5 in each
Mar 31 of Sr_Obj") element of
Apr 30 print(Sr_Obj/5) Sr_Obj
May 31 A 2.0
print("Display First 6 Display First 6 Rows B 4.0
Rows") Jan 31 C 6.0
print(Sr_Obj.head(6)) Feb 28 D 8.0
Mar 31 print(Sr_Obj>20) A False
Apr 30 B False
May 31 C True
Jun 30 D True
print("Display Last 2 Display Last 2 Rows print("Sr_Obj**2") A 100
Rows") Jun 30 print(Sr_Obj**2) B 400
print(Sr_Obj.tail(2)) Jul 31 C 900
print("Display Last 5 Display Last 5 Rows D 1600
Rows") Mar 31 #Adding two Series of similar indexes
print(Sr_Obj.tail()) Apr 30 import numpy as np
May 31 import pandas as pd
Jun 30 class11=pd.Series(data=[30,40,50],index=['scien
Jul 31 ce','arts','commerce'])
print("Display Last 6 Display Last 6 Rows class12=pd.Series(data=[60,80,100],index=['scie
Rows") Feb 28 nce','arts','commerce'])
8Page
|K V4
S REGIONAL OFFICE, JAIPUR |SUBJECT-INFORMATICS PRACTICES (TERM -I SESSION
2021-22)
print("Total number of students") D 100
print(class11+class12) Sorting Series Values based on Indexes
Sr_Obj.sort_index() OR A 200
Output: Sr_Obj.sort_index(ascending= B 400
Total number of students True) C 300
science 90 D 100
arts 120 Sr_Obj.sort_index(ascending= D 100
commerce 150 False) C 300
#Adding two Series of dissimilar indexes B 400
class11=pd.Series(data=[30,40,50],index=['scien A 200
ce','arts','commerce']) Arithmetic on Series
class12=pd.Series(data=[60,80,100],index=['sci',
import pandas as pd Addition of
'arts','commerce'])
import numpy as np Series-s1+s2:
print("Total number of students")
s1=pd.Series(data=[20,40,60], A 22
print(class11+class12)
index=['A','B','C']) B 44
s2=pd.Series(data=[2,4,6], C 66
Output:
index=['A','B','C'])
Total number of students
print("Addition of Series:
arts 120.0
s1+s2")
commerce 150.0
print(s1+s2)
sci NaN
print("Division of Series: s1/s2") Division of
science NaN
print(s1/s2) Series: s1/s2
A 10.0
Filtering Entries of Series
B 10.0
import pandas as pd
C 10.0
Info=pd.Series(data=[31,41,51]) info>40
0 False
print("Addition of Series: Addition of
p \ 1 True
S3=s1+s2") Series:
2 True
s3=s1+s2 S3=s1+s2
p \ info[info>40]
print(s3) A 22
info[info>40]) 1 41
B 44
2 51
C 66
Sorting Series Values based on Values NumPy Arrays Vs Series Object
import pandas as pd D 100
1. In ndarray, vector operations can only be
import numpy as np A 200
performed if shape of both array match,
C 300
otherwise it will generate error.
B 400
2. In Series, vector operations can have
data=[200,400,300,100]) (By default
performed with different shapes series
Sr_Obj.sort_values() OR order is
also. For different shape series operation
Sr_Obj.sort_values(ascending= Ascending)
gives NaN values.
True)
3. In ndarray, the indexes always numeric
and start with 0 onwards. But in series,
Sr_Obj.sort_values(ascending= B 400 indexes can have any type of indexes.
False) C 300
A 200

Page
9 | K V S 5R E G I O N A L
2021-22)
OFFICE, JAIPUR |SUBJECT-INFORMATICS PRACTICES (TERM -I SESSION

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy