DataFrame Objects
Selecting or Accessing Data
import pandas as pd
dict={"Population":[1632, 2354, 3322, 4242],"Hospitals":[5454, 4545,
5428,9958],"Schools":[5442,5745,5412,5812]}
dtf=pd.DataFrame(dict,index=["Delhi","Mumbai","Kolkata","Chennai
"])
print(dtf)
OutPut
Population Hospitals Schools
Delhi 1632 5454 5442
Mumbai 2354 4545 5745
Kolkata 3322 5428 5412
Chennai 4242 9958 5812
It returns values for selected column
print(dtf["Population"])
OutPut
Delhi 1632
Mumbai 2354
Kolkata 3322
Chennai 4242
Name: Population
It returns values for Multiple columns
print(dtf[["Population","Hospitals"]])
OutPut
Population Hospitals
Delhi 1632 5454
Mumbai 2354 4545
Kolkata 3322 5428
Chennai 4242 9958
Selecting or Accessing a Subset from a DataFrame using
Row/Column names
To Access a row
print(dtf.loc["Delhi",:])
OutPut
Population 1632
Hospitals 5454
Schools 5442
Name: Delhi,
To Access a Multiple row
Objname.loc[:, Start column:End column]
print(dtf.loc["Mumbai":"Kolkata",:])
OutPut
Population Hospitals Schools
Mumbai 2354 4545 5745
Kolkata 3322 5428 5412
To Access selective columns
All columns falling b/w start & end columns are listed
print(dtf.loc[ :,"Population":"Schools"])
OutPut
Population Hospitals Schools
Delhi 1632 5454 5442
Mumbai 2354 4545 5745
Kolkata 3322 5428 5412
Chennai 4242 9958 5812
To access range of columns from a range of rows
Selecting a range of columns from a range of rows
Syntax :- Objname.loc [Start row: End row, Start column:End
column]
print(dtf.loc["Delhi":"Mumbai","Population":"Hospitals"])
OutPut
Population Hospitals
Delhi 1632 5454
Mumbai 2354 4545
Selecting Row/Column from a DataFrame
Syntax :- Objname.iloc[ Start row index : End row index, Start
column index : End column index]
print(dtf.iloc[0:2,1:3])
OutPut
Hospitals Schools
Delhi 5454 5442
Mumbai 4545 5745
Adding /Modifying columns value
To add column
Syntax :- objectname[columnname]=newValue
dtf['Density'] = 1219
print(dtf)
OutPut
Population Hospitals Schools Density
Delhi 1632 5454 5442 1219
Mumbai 2354 4545 5745 1219
Kolkata 3322 5428 5412 1219
Chennai 4242 9958 5812 1219
dtf['Density'] = [1219,15478,9874,8451]
print(dtf)
OutPut
Population Hospitals Schools Density
Delhi 1632 5454 5442 1219
Mumbai 2354 4545 5745 15478
Kolkata 3322 5428 5412 9874
Chennai 4242 9958 5812 8451
Adding /Modifying rows
Syntax :-objectname[rowname]=newValue
dtf.loc['Bangalore’] =1255
print(dtf)
OutPut
Population Hospitals Schools
Delhi 1632 5454 5442
Mumbai 2354 4545 5745
Kolkata 3322 5428 5412
Chennai 4242 9958 5812
Bangalore 1255 1255 1255
Modifying a Single Cell
Syntax :- ObjectName.columnName[rowname]=newValue
dtf.Population["Kolkata”] =99999
print(dtf)
OutPut
Population Hospitals Schools
Delhi 1632 5454 5442
Mumbai 2354 4545 5745
Kolkata 99999 5428 5412
Chennai 4242 9958 5812
Deleting Rows/Columns
del dtf['Schools']
print(dtf)
OutPut
Population Hospitals
Delhi 1632 5454
Mumbai 2354 4545
Kolkata 3322 5428
Chennai 4242 9958