Pandas - I (PPT 6)
Pandas - I (PPT 6)
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.
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.