0% found this document useful (0 votes)
6 views11 pages

Pandas Dataframe Activity - Removed - Removed (1) - Removed

Uploaded by

maddiadarsh
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)
6 views11 pages

Pandas Dataframe Activity - Removed - Removed (1) - Removed

Uploaded by

maddiadarsh
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/ 11

Visit Python4csip.

com for more updates


DATAFRAME

DATAFRAME-It is a two-dimensional
DATAFEAME object that is useful in
representing data in the form of rows and columns. It is similar to a
spreadsheet or an SQL table. This is the most commonly used pandas
object. Once we store the data into the Dataframe, we can perform
various operations that are useful in analyzing and understanding the
data.

DATAFRAME STRUCTURE

COLUMNS PLAYERNAME IPLTEAM BASEPRICEINCR

0 ROHIT MI 13

1 VIRAT RCB 17

2 HARDIK MI 14

INDEX DATA

PROPERTIES OF DATAFRAME
DATAFEAME
1. A Dataframe has axes (indices)-
➢ Row index (axis=0)
➢ Column index (axes=1)
2. It is similar to a spreadsheet , whose row index is called index and
column index is called column name.
3. A Dataframe contains Heterogeneous data.
4. A Dataframe Size is Mutable.
5. A Dataframe Data is Mutable.

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates
A data frame can be created using any of the following-

1. Series
2. Lists
3. Dictionary
4. A numpy 2D array

How to create Dataframe From Series


DATAFEAME
Program-
Output-
import pandas as pd
0
s = pd.Series(['a','b','c','d']) 0 a
1 b Default Column Name As 0
df=pd.DataFrame(s)
2 c
print(df) 3 d

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

DataFrame from Dictionary of Series

Example-

DataFrame from List of Dictionaries

Example-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

Select operation in data frame

To access the column data ,we can mention the column name as
subscript.
e.g. - df[empid] This can also be done by using df.empid.
To access multiple columns we can write as df[ [col1, col2,---] ]

Example -

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

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=[‘List1’] To Rename the default column of Data


Frame as List1

df[‘List2’]=20 To create a new column List2 with all values


as 20

df[‘List3’]=df[‘List1’]+df[‘List2’] Output-

Add Column1 and Column2 and store in List1 List2 List3


0 10 20 30
New column List3 1 15 20 35
2 18 20 38
print(df) 3 22 20 42

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

To Delete a Column in data frame

We can delete the column from a data frame by using any of


the the following –
1. del
2. pop()
3. drop()

>>del df[‘List3’] We can simply delete a column by passing


column name in subscript with df
>>df
Output-

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

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

To Delete a Column Using drop()

import pandas as pd
s= pd.Series([10,20,30,40])
df=pd.DataFrame(s)
df.columns=[‘List1’]
df[‘List2’]=40
df1=df.drop(‘List2’,axis=1) (axis=1) means to delete Data
column wise
df2=df.drop(index=[2,3],axis=0) (axis=0) means to delete
data row wise with given index
print(df)
print(“ After deletion::”)
print(df1)
print (“ After row deletion::”)
print(df2)

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
CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

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.

Accessing the data frame through loc()

It is used to access a group of rows and columns.

Syntax-

Df.loc[StartRow : EndRow, StartColumn : EndColumn]


Note -If we pass : in row or column part then pandas provide the entire
rows or columns respectively.

To access a single row

To access multiple Rows Qtr1 to Qtr3

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

Accessing the data frame through iloc()

It is used to access a group of rows and columns based on numeric


index value.

Syntax-

Df.loc[StartRowindexs : EndRowindex, StartColumnindex : EndColumnindex]

Note -If we pass : in row or column part then pandas provide


the entire rows or columns respectively.

To access First two Rows


and Second column

To access all Rows and First


Two columns Record

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
head() andVisittail() Method
Python4csip.com for more updates

The method head() gives the first 5 rows and the method
tail() returns the last 5 rows.
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())
print(df.tail())
Output-
Doj empid ename
0 12-01-2012 101 Sachin
1 15-01-2012 102 Vinod
2 05-09-2007 103 Lakhbir Data Frame
3 17-01-2012 104 Anil
4 05-09-2007 105 Devinder
5 16-01-2012 106 UmaSelvi
Doj empid ename
0 12-01-2012 101 Sachin
1 15-01-2012 102 Vinod head() displays first 5 rows
2 05-09-2007 103 Lakhbir
3 17-01-2012 104 Anil
4 05-09-2007 105 Devinder
Doj empid ename
1 15-01-2012 102 Vinod
2 05-09-2007 103 Lakhbir
3 17-01-2012 104 Anil tail() display last 5 rows
4 05-09-2007 105 Devinder
5 16-01-2012 106 UmaSelvi
CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
Visit Python4csip.com for more updates

Boolean Indexing in Data Frame

Boolean indexing helps us to select the data from the DataFrames


using a boolean vector. We create a DataFrame with a boolean index to
use the boolean indexing.

To Return Data frame where index is True

We can pass only integer value in iloc

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR

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