Pandas Class 12 Ncertttt
Pandas Class 12 Ncertttt
Pandas Class 12 Ncertttt
Pandas:
It is a package useful for data analysis and manipulation.
Pandas provide an easy way to create, manipulate and
wrangle the data.
Pandas provide powerful and easy-to-use data structures, as
well as the means to quickly perform operations on these
structures.
1. Series
2. Data Frame
3. Panel
e.g.-
Index Data
0 10
1 15
2 18
3 22
import pandas as pd
Output-
importnumpy as np Default Index
0 10
arr=np.array([
10,15,18,22])
1 15
s = pd.Series(arr
) 2 18
print(
s) 3 22
Data
Here we createan
array of 4 values.
How to create Series with
Mutable index
Program
-
print(
s)
Print all the values of the Series that are greater than
2.
Example-2
Result of s.head()
Result of s.head(3)
Series provides index label loc and ilocand [] to access rows and
columns.
Syntax:-series_name.loc[StartRange: StopRange]
Example-
Example-
3. Selection Using [] :
Syntax:-series_name[StartRange> : StopRange] or
series_name[ index]
Example
-
Example-
Slicing in Series
Slicing is a way to retrieve subsets of data from a pandas object. A
slice object syntax is –
DATAFRAME STRUCTURE
COLUMNS PLAYERNAME IPLTEAM BASEPRICEIN
CR
0 ROHIT MI 13
1 VIRAT RCB 17
2 HARDIK MI 14
INDEX DATA
PROPERTIES OF DATAFRAME
2 c
3 d
Example-
1. iterrows ()
2. iteritems ()
iterrows()
iteritems()
Example-
Select operation in data frame
> df[[‘empid’,’ename’]]
> empid ename
0 101 Sachin
1 102 Vinod
2 103 Lakhbir
3 104 Anil
4 105 Devinder
5 106 UmaSelvi
To Add & Rename a column in
data frame
import pandas as
pd
s =pd.Series([10,15,18,22
])
df=pd.DataFrame(
s)
df.columns=[‘List To Rename the
default column of
1’] Frame as Data
List1
df[‘List2’]= To create a new column List2 with all values
20 as 20
df[‘List3’]=df[‘List1’] Output-
+df[‘List2’]
List1 List2 List3
Add Column1 and Column2 and
store in 0 10 20 30
New column 1 15 20 35
List3 2 18 20 38
print(df 3 22 20 42
)
To Delete a Column in data frame
List1 List2
0 10 20
1 15 20
2 18 20
3 22 20
>>df.pop(‘List2
we can simply delete a column by
’)
passing column name in pop method.
>>df
List1
0 10
1 15
2 18
3 22
Output-
List1 List2
0 10 40
1 20 40
2 30 40
3 40 40
After deletion::
List1
0 10
1 20
2 30
3 40
After row deletion::
List1
0 10
1 20
Accessing the data frame through
loc() and iloc() method or indexing
using Labels
Pandas provide loc() and iloc() methods to access the subset from a
data frame using row/column.
Syntax-
Syntax-
The method head() gives the first 5 rows and the method
tail() returns the last 5 rows.
To display first 2 rows we can use head(2) and to returns
last2 rows we can use tail(2) and to return 3 rd to 4th row
we can write df[2:5].
import pandas as pd empdata={ 'Doj':['12-01-
2012','15-01-2012','05-09-2007',
'17-01-2012','05-09-2007','16-01-2012'],
'empid':[101,102,103,104,105,106],
'ename':['Sachin','Vinod','Lakhbir','Anil','Devinder','UmaSelvi']
}
df=pd.DataFrame(empdata)
print(df)
print(df.head(2))
print(df.tail(2))
print(df[2:5])
Output-
Doj empid ename
0 12-01-2012 101 Sachin
1 15-01-2012 102 Vinod
2 05-09-2007 103 Lakhbir
3 17-01- 2012 104 Anil
4 05-09-2007 105 Devinder
5 16-01-2012 106 UmaSelvi
Example-1
This will give the common rows between the
two data frames for the corresponding column
values
(‘id’).
Exampl-2
e
Example-
Example-
3. RightJoin :-The right join produce a complete set of
records from data frame B(Right side Data Frame) with the
matching records (where available) in data frame A( Left side data
frame). If there is no match right side will contain null. You have to
pass right in how argument inside merge() function.
Example-
4.Left Join :- The left join produce a complete set of records
from data frame A(Left side Data Frame) with the matching
records (where available) in data frame B( Right side data frame).
If there is no match left side will contain null. You have to pass left
in how argument inside merge() function.
Example-
5. Joining on Index :-Sometimes you have to perform the
join on the indexes or the row labels. For that you have to specify
right _index( for the indexes of the right data frame ) and left_
index( for the indexes of left data frame) as True.
Example-
CSV File