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

Pandas - I (PPT 6)

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)
18 views

Pandas - I (PPT 6)

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/ 14

NAND VIDYA NIKETAN

JAMNAGAR

GRADE – XII
INFORMATICS PRACTICES
PREPARED BY : VIPITHA V
CONTACT NO : 8547196016
Selecting / Accessing Individual Values
To select/access an individual data value from a dataframe, you can use any of
the following methods:-
(i) Either give name of row or numeric index in square brackets with,
i.e.,
<DF Object>.<column> [<row name or row numeric index>]
import pandas as pd
Popu=[10927986,12691836,4631392,4328063]
Hos=[189,208,149,157]
Sch=[7916,8508,7221,7613]
Dict={'Population':Popu,'Hospitals':Hos,'Schools':Sch}
DFObj=pd.DataFrame(Dict,
columns=['Population','Hospitals','Schools'],index=['Delhi','Mumbai','Kolkata'
, 'Chennai'])
print(DFObj)
Output
Population Hospitals Schools
Delhi 10927986 189 7916
Mumbai 12691836 208 8508
Kolkata 4631392 149 7221
Chennai 4328063 157 7613

print(DFObj.Population['Delhi’])
10927986
print(DFObj.Population[2])
4631392
(ii) You can use at or iat attributes with DF object as shown below:-
Use To
<DFObject>.at[<row label> <col Access a single value for a
label>] row/column label pair.
<DFObject>.iat[<row index no.> Access a single value for a
<col index no.>] row/column pair by integer
position.

Consider examples given below:-


print (DFObj.at['Mumbai','Hospitals’])
208
print (DFObj.iat[3,2])
7613
Adding/Modifying Rows’/Columns’ Values in
DataFrames
You can assign or modify data in a dataframe in the same way
as we do with other objects. All you need is to specify the row
name and/or column name along with the dataframe’s name.
Adding/Modifying a Column
Columns in a dataframe can be referred to multiple ways.
Assigning a value to a column:-
❖ Will modify it, if the column already exists.
❖ Will add a new column, if it does not exist already.
To change / add a column, use syntax:-
<DF Obect>.<column name> = <new value>
Or
<DF Obect>.<column> = <new value>
For Example:-
DFObj['Density']=1250
print(DFObj) Output
Population Hospitals Schools Density
Delhi 10927986 189 7916 1250
Mumbai 12691836 208 8508 1250
Kolkata 4631392 149 7221 1250
Chennai 4328063 157 7613 1250
DFObj['Density']=[1500,1289,1630,1050]
print(DFObj)
Output
Population Hospitals Schools Density
Delhi 10927986 189 7916 1500
Mumbai 12691836 208 8508 1289
Kolkata 4631392 149 7221 1630
Chennai 4328063 157 7613 1050
Same way, you can modify an existing column by assigning a new list of
values to it. There are some other ways of adding a column to a
dataframe. They are:
<DF Object>.at[:, <column name>]=<values for column>
<DF Object>.loc[:, <column name>]=<values for column>
<DF Object>.assign(<column name>=[<values for column>])
Adding/Modifying a Row
Like columns, you can add or change rows to a dataframe using at or loc
attributes as explained below:-
To change/add row, use syntax:-
<DF Object>.at[<row name>,:]=<new value>
<DF Object>.loc[<row name>,:]=<new value>
Likewise, if there is no row with such row label, then Python adds a new row
with this row label and assigns given values to all columns.
DFObj.at['Banglore', :]=1200
print (DFObj) Output
Population Hospitals Schools
Delhi 10927986.0 189.0 7916.0
Mumbai 12691836.0 208.0 8508.0
Kolkata 4631392.0 149.0 7221.0
Chennai 4328063.0 157.0 7613.0
Banglore 1200.0 1200.0 1200.0
DFObj.loc['Banglore', :]=[1200,1500,1300]
print (DFObj)

Output
Population Hospitals Schools
Delhi 10927986.0 189.0 7916.0
Mumbai 12691836.0 208.0 8508.0
Kolkata 4631392.0 149.0 7221.0
Chennai 4328063.0 157.0 7613.0
Banglore 1200.0 1500.0 1300.0
Modifying a Single Cell
You may use <DataFrame>.iat[<row position>,<column position>]
to modify values using row and column position. To change/modify
a single data value, use syntax:-
<DF>.<column name>[<row name/label>]=<new/modified value>
DFObj.Population['Banglore']=56234
print (DFObj)
Population Hospitals Schools Density
Delhi 10927986.0 189.0 7916.0 1500.0
Mumbai 12691836.0 208.0 8508.0 1289.0
Kolkata 4631392.0 149.0 7221.0 1630.0
Chennai 4328063.0 157.0 7613.0 1050.0
Banglore 56234.0 1500.0 1300.0 1600.0
Deleting Rows/Columns in a DataFrame
Python Pandas provides two ways to delete rows and columns. del statement
and drop( ) function. To delete a column, you use del statement like this:-
del <DF Object> [<column name>]
For Example:-
del DFObj['Density']
print (DFObj)
Output
Population Hospitals Schools
Delhi 10927986.0 189.0 7916.0
Mumbai 12691836.0 208.0 8508.0
Kolkata 4631392.0 149.0 7221.0
Chennai 4328063.0 157.0 7613.0
Banglore 56234.0 1500.0 1300.0
To delete rows from dataframe, you can use
<DF Object>.drop(index or sequences of indexes)
For example, the below commands will delete the rows
with indexes 2,4,6,8,12 from dataframe DFObj.
DFObj.drop(range(2,13,2))
DFObj.drop([2,4,6,8,12])
You can also give axis1=1 along with indexes/labels then
drop() will drop the columns . For Example:
DFObj.drop([“Total Cost”, “Order ID”], axis = 1) --→ is
used for deleting the columns Total Cost and Order ID
from the DataFrame DFObj.
Renaming Rows/Columns
To change the name of any row/column individually, you can use the
rename() function of DataFrame as per the syntax given below:-
<DFObj>.rename(index= {<names dictionary>}, columns= {<names
dictionary>}, inplace=False) where ,
➢ The index argument is for index names (row labels). If you want to rename
rows only then specify only index argument.
➢ The columns argument is for columns names. If you want to rename
columns only then specify only columns argument.
➢ For both index and columns arguments, specify the names-change
dictionary containing original names and new names in form like {old
name:new name}
➢ Specify inplace argument as True if you want to rename the rows/columns
in the same dataframe as if you skip this argument, then a new dataframe is
created with new indexes /columns’ names and original remains unchanged.

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