0% found this document useful (0 votes)
30 views24 pages

PANDAS - PPT 32q

It is a file

Uploaded by

dhiyu00
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)
30 views24 pages

PANDAS - PPT 32q

It is a file

Uploaded by

dhiyu00
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/ 24

NAND VIDYA NIKETAN

JAMNAGAR
GRADE – XII
INFORMATICS PRACTICES
PREPARED BY: VIPITHA V
CONTACT NO : 8547196016
Naming a Series
We can also give a name to the entire series, i.e., its two
columns, index and values of a series using ‘name’
property.
import pandas as pd
S1=pd.Series({‘Jan’:31, ‘Feb’:28, ‘Mar’:31, ‘Apr’:30})
S1.name=‘Days of Month’
S1.index.name=‘Month’
print(S1)
Output
Month
Jan 31
Feb 28
Mar 31
Apr 30
Name : Days of Month, dtype: int 64
SERIES OBJECT ATTRIBUTES
Attributes Description
Series.index Returns index of the series
Series.values Returns ndarray
Series.dtype Returns dtype object of the underlying data
Series.shape Returns tuple of the shape of underlying data
Series.nbytes Returns number of bytes of underlying data
Series.ndim Returns the number of dimensions(axis)
Series.size Returns number of elements
Series.itemsize Returns the size of the dtype
Series.hasnans Returns true if there are any NaN
Series.empty Returns true if series object is empty
#ILLUSTRATION CODE FOR THE ABOVE TABLE:
>>> import pandas as pd
>>> s = pd . Series(range(1,15,3), index=[x for x in 'abcde'])
>>> s . index
index ( ['a', 'b', 'c', 'd', 'e'], dtype = 'object')
>>> s . values
array ([ 1, 4, 7, 10, 13], dtype = int64)
>>> s . shape
(5,)
>>> s . size
5
>>> s . nbytes
40
>>> s.nbytes
40
>>> s.ndim
1
>>> s.itemsize
8
>>>s.empty
False
>>>s.hasnans
False
If you use len( ) on a Series Object,
then it returns total number of
elements in it including NaNs but
<series>. count( ) returns only the
count of non-NaN values in a Series
Object.
OPERATIONS ON SERIES OBJECT
1. Modifying elements of Series Object
The data values of a Series object can be easily modified through item assignment, i.e.,
<series object> [<index>] = <new data_value>
For Example:-
import pandas as pd
SObj=pd.Series([10,14,16,18,20])
SObj[3]=100
SObj[:2]= 50
print (SObj)
The Output will be:-
0 50
1 50
2 16
3 100
4 20
dtype: int64
2. HEAD( ) AND TAIL( ) FUNCTIONS
The Series . head ( ) function in a series fetches the
first ‘n’ from a pandas object. By default, it gives up the
top 5 rows of data in the series. On the contrary, Series
. tail ( ) function displays the last 5 elements by
default. However we can pass the number as
arguments for the number of values to be pulled out
from the series and pandas shall print out the specified
number of rows.
#using head ( ) and tail ( ) functions on
series.
import pandas as pd
s=pd.Series([10,20,30,40,50],
index=['a','b','c','d','e'])
print (s)
print (s.head(3))
print (s.tail(3))
The output will be:
a 10
b. 20
c. 30
d. 40
e. 50
dtype: int64
a 10
b. 20
c. 30
dtype: int64
c. 30
d. 40
e. 50
dtype: int64
3. VECTOR OPERATIONS ON SERIES:
Series also supports vector operations. Any operation to be
performed on a series gets performed on every single element on it.
For Eg:
>>>s >>>s+2 >>>s*3 >>>s**2
1 10 1 12 1 30 1 100
2 11 2 13 2 33 2 121
3 12 3 14 3 36 3 144
4 13 4 15 4 39 4 169
5 14 5 16 5 42 5 196
4. MATHEMATICAL OPERATIONS ON SERIES
Mathematical processing can be performed on series using scalar
values and functions. All the arithmetic operators ( +, -, *, / ) etc. can
be successfully performed on series.
import pandas as pd
s=pd.Series ([10, 20, 30, 40, 50])
s1=pd.Series ([20, 30, 40, 50, 60])
s2= pd.Series ([100, 200, 300, 400, 500])
print(s)
print(s+1)
print(s+s1)
print(s2-s1)
print(s2*s1)
The output will be:
0 10
1 20
2 30
3 40
4 50
dtype: int64
0 11
1 21
2 31
3 41
4 51
dtype: int64
0 30
1 50
2 70
3 90
4 110
dtype: int64
0 80
1 170
2 260
3 350
4 440
dtype: int64
0 2000
1 6000
2 12000
3 20000
4 30000
5. FILTERING ENTRIES
We can filter out entries from a Series Object using expressions that
are of Boolean type, (i.e., the expressions that yield a Boolean
Value) as per the following syntax:-
<Series Object> [ [<Boolean Expression on Series Object>] ]
For Eg:-
import pandas as pd
s=pd.Series([10,20,30,40,50])
print(s)
print(s<30)
print(s>=40)
print (s[s<=20])
The output will be:
0 10
1 20
2 30
3 40
4 50
dtype: int64
1 True
2 True
3 False
4 False
5False
dtype: bool
1 FALSE
2 FALSE
3 FALSE
4 TRUE
5TRUE
dtype: bool
0 10
1 20
dtype: int64
SORTING SERIES VALUES
We can sort the values of a Series object on the basic of
values and indexes.
Sorting on the Basic of Values
To sort a Series Object on the basic of values, you may use
sort_values( ) function as per the following syntax:-
<Series object>.sort_values ([ascending = True / False])
The argument ascending is optional and if skipped, it takes
the value True by default. It means , the sort_values()
arranges the values in a Series object in ascending order by
default.
For Eg:- Consider the Series Object Sobj,
>>>Sobj
A 1600
B 2300
C 3870
D 1400
>>>Sobj.sort_values( ) ---→ Values sorted in ascending order by default
D 1400
A 1600
B 2300
C 3870
>>>Sobj.sort_values(ascending = False)
C 3870
B 2300
A 1600
D 1400
To sort values in descending order, you may write
<Series>.sort_values(ascending=False)
Sorting on the Basic of Indexes
To sort a Series Object on the basic of indexes, you may use sort_index( ) function as
per the following syntax:-
<Series object>.sort_index([ascending = True / False])
The argument ascending is optional and if skipped, it takes the value True by default.
For Eg:- Consider the Series Object Sobj,
>>>Sobj
A 1600
B 2300
C 3870
D 1400
>>>Sobj.sort_index(ascending = False)
D 1400
C 3870
B 2300
A 1600
DELETING ELEMENTS FROM A SERIES:
We can delete an element from a series using drop ( ) method by
passing the index of the element to be deleted as the argument to it.
>>> import pandas as pd
>>> s=pd.Series ([1,2,3,4,5])
>>> s.drop(3)
The Output will be:
0 1
1 2
2 3
4 5
dtype: int64
DIFFERENCE BETWEEN NUMPY ARRAYS AND SERIES OBJECT

1. In case of ndarrays, we can perform vectorized operations only


if the shapes of two ndarrays match, otherwise it returns an
error. But with Series object, in case of vectorized operations,
the data of two series objects is aligned as per matching indexes
and operation is performed on them and for non-matching
indexes, NaN is returned.
2. In ndarrays, the indexes are always numeric starting from 0
onwards. But Series objects can have any type of indexes,
including numbers (not necessary starting from 0 ), letters,
labels , strings etc.

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