[go: up one dir, main page]

0% found this document useful (0 votes)
46 views9 pages

Loc Iloc at Dataframe

Uploaded by

aneesh.pai
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)
46 views9 pages

Loc Iloc at Dataframe

Uploaded by

aneesh.pai
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/ 9

DATAFRAME -Operations

Selecting or Accessing data:


You can extract or select desired rows and columns as per your requirement
from a dataframe. Consider the dataframe dtf5

dtf5
Population Hospitals Schools
Delhi 10927986 189 7916
Mumbai 12691836 208 8508
Kolkata 4631392 149 7226
Chennai 4328063 157 7617

Selecting/Accessing a column
Syntax: Dataframeobject[columnname] OR Dataframeobject.columnname

Example: dtf5[‘Population’]
OR
dtf5.Population
Delhi 10927986
Mumbai 12691836
Kolkata 4631392
Chennai 4328063

Selecting /Accessing multiple columns


Dataframeobject[[colmnname1, colmnname2, colmname3…]]
Ex: dtf5[[‘Schools’, ‘Hospitals’]]
Schools Hospitals
Delhi 7916 189
Mumbai 8508 208
Kolkata 7226 149
Chennai 7617 157
Selecting/Accessing a subset from a dataframe using row/column
names
To access rows or a combination of rows and columns, you can use following
syntax
Dataframeobject.loc[startrow:endrow, startcolumn:endcolumn]
To access a row:
Dataframeobject.loc[rowlabel,:]
Ex:
dtf5.loc[‘Delhi’, :]
Output:
Population 10927986
Hospitals 189
Schools 7916
Name: Delhi, dtype: int64
To access multiple rows:
dtf5.loc[‘Mumbai’: ‘Kolkata’, :]
Population Hospitals Schools
Mumbai 12691836 208 8508
Kolkata 4631392 149 7226

To access selective columns:


dtf5.loc[:, ‘Population’:’Schools’]
Population Hospitals Schools
Delhi 10927986 189 7916
Mumbai 12691836 208 8508
Kolkata 4631392 149 7226
Chennai 4328063 157 7617

To access range of columns from a range of rows:


dtf5.loc[‘Delhi’:’Mumbai’, ‘Population’:’Hospitals’]
Population Hospitals
Delhi 10927986 189
Mumbai 12691836 208

To access range of columns from a range of rows: using iloc (integer


location)
Dataframeobject.iloc[startrowindex:endrowindex, startcolindex:endcolindex]
Ex: dtf5.iloc[0:2, 1:3])
Hospitals Schools
Delhi 189 7916
Mumbai 208 8508

Selecting/Accessing individual value


To select and access individual data value from a dataframe
i) Either give name of row or numeric index in square brackets
df.columnname[rowname or rowindex]
example:
dtf5.Population[‘Delhi’] or dtf5.Population[1]
ii) You can use at or iat
Syntax: Dataframeobject.at[rowlabel,columnlabel]
Dataframeobject.iat[rowindno,colindno]
print(df.at['Delhi', 'Population'])
print(df.iat[3,2])
Selecting dataframe rows/columns based on Boolean condition
Apply boolean condition on dataframe If you want to select data based on
condition with in a dataframe.
Syntax: Dataframeobject[ condition] OR Dataframeobject.loc[ condition]
Eg:
dtf5[‘Schools’>7500]
Delhi True
Mumbai True
Kolkata False
Chennai True
Name: Schools, dtype: bool
But applying conditions will just give the result in terms of true or false.To
yield values based on condition, just write condition inside square bracket
next to dataframe object
Ex:
print(df[df['Schools']>7500])
Population Hospitals Schools
Delhi 10927986 189 7916
Mumbai 12691836 208 8508
Chennai 4328063 157 7617

Adding/Modifying row/column values in a dataframe

Adding/Modifying column:
Assigning a value to column

• Will modify it, if the column already exist


• Will add a new column, if it does not exist already

Adding/Modifying column
Dataframeobject[columnname] =new value
OR
Dataframeobject.columnname = newvalue

Eg: df['Schools']=7800
Population Hospitals Schools
Delhi 10927986 189 7800
Mumbai 12691836 208 7800
Kolkata 4631392 149 7800
Chennai 4328063 157 7800
df['Healthcenter']=[1500, 1700, 1800,1900] {This will add a new column to
dataframe with value 1500
Population Hospitals Schools Healthcenter
Delhi 10927986 189 7800 1500
Mumbai 12691836 208 7800 1700
Kolkata 4631392 149 7800 1800
Chennai 4328063 157 7800 1900

Other ways of adding column values


Dataframeobject.at[:, colname] = values
Dataframeobject.loc[:, colname] = values
Dataframeobject= Dataframeobject.assign(colname=values)

Adding/Modifying a row
Dataframeobject.at[rowname, :] = values
Dataframeobject.loc[rowname, :] = values
Ex:
df.at['Bangalore']=1200

Population Hospitals Schools Healthcenter


Delhi 10927986.0 189.0 7800.0 1500.0
Mumbai 12691836.0 208.0 7800.0 1500.0
Kolkata 4631392.0 149.0 7800.0 1500.0
Chennai 4328063.0 157.0 7800.0 1500.0
Bangalore 1200.0 1200.0 1200.0 1200.0

Modifying a single cell


dataframeobject.columnname[rowname]=new value

eg: dtf5.Healthcenter['Bangalore'])=999
Shallow vs Real copy

When a shallow copy of a DataFrame or Series object is created, it doesn’t


copy the indices and the data of the original object but it simply copies the
references to its indices and data. As a result of which, a change made to
one is reflected in the other one.
There is an additional argument copy which is set as false by deafult. With
copy as False, the new dataframe is not created as a separate copy and is
thus linked to original df. Hence all changes made to original df will be
reflected in other too. In ordre to create a df that is completely unlinked ,
you need to set copy as true (true copy or real copy)

df1=dtf5
print(df1)
dtf5.loc['Chennai','Hospitals']=858
print(dtf5)
print(df1)

dtf5
Population Hospitals Schools Healthcenter
Delhi 10927986.0 189.0 7800.0 1500.0
Mumbai 12691836.0 208.0 7800.0 1500.0
Kolkata 4631392.0 149.0 7800.0 1500.0
Chennai 4328063.0 858.0 7800.0 1500.0
Bangalore 1200.0 1200.0 1200.0 999.0
df1
Population Hospitals Schools Healthcenter
Delhi 10927986.0 189.0 7800.0 1500.0
Mumbai 12691836.0 208.0 7800.0 1500.0
Kolkata 4631392.0 149.0 7800.0 1500.0
Chennai 4328063.0 858.0 7800.0 1500.0
Bangalore 1200.0 1200.0 1200.0 999.0
df2=pd.DataFrame(dtf5, copy=True)
dtf5.Schools['Chennai']=1111
print(dtf5)
print(df2)

dtf5
Population Hospitals Schools Healthcenter
Delhi 10927986.0 189.0 7800.0 1500.0
Mumbai 12691836.0 208.0 7800.0 1500.0
Kolkata 4631392.0 149.0 7800.0 1500.0
Chennai 4328063.0 858.0 1111.0 1500.0
Bangalore 1200.0 1200.0 1200.0 999.0

df2
Population Hospitals Schools Healthcenter
Delhi 10927986.0 189.0 7800.0 1500.0
Mumbai 12691836.0 208.0 7800.0 1500.0
Kolkata 4631392.0 149.0 7800.0 1500.0
Chennai 4328063.0 858.0 7800.0 1500.0
Bangalore 1200.0 1200.0 1200.0 999.0

Deleting /renaming a column/rows

Two ways to delete rows and columns -del statement and drop(). Pandas
provide rename() function to rename rows and columns.

Deleting rows/columns in a dataframe

• To delete a column, Use del statement : del dataframeobject[colname]


del dtf5[‘Healthcenter’]
Population Hospitals Schools
Delhi 10927986.0 189.0 7800.0
Mumbai 12691836.0 208.0 7800.0
Kolkata 4631392.0 149.0 7800.0
Chennai 4328063.0 157.0 7800.0
Bangalore 1200.0 1200.0 1200.0

To delete a row, Use drop statement


dataframeobject.drop(index)
ex: df.drop(2,4) (this will delete rows with index) or df.drop(‘hospitals,
axis=1)
Renaming rows/columns
dataframeobject.rename(index={names}, columns={names}, inplace=True)

let us see how rename works


dtf5.rename(index={‘Delhi’: ‘Tamilnadu’, ‘Mumbai’: ‘Hyderabad’}
column={‘Hospitals’:’Clinic’}, inplace=True)

Population Clinic Schools


Tamilnadu 10927986.0 189.0 7800.0
Hyderabad 12691836.0 208.0 7800.0
Kolkata 4631392.0 149.0 7800.0
Chennai 4328063.0 157.0 7800.0
Bangalore 1200.0 1200.0 1200.0

Dataframe Indexing : Creating dataframe with Boolean index


import pandas as pd
days=[‘Mon’, ‘Tue’, ‘Wed’, ‘Thurs’, ‘Fri’]
classes=[6,0, 3,0, 8]
dc={‘Days’: Days, ‘No of classes’:classes}
clasdf=pd.DataFrame(dc, index=[True, False, True, False, True])
Days No of classes
True Mon 6
False Tue 0
True Wed 3
False Thurs 0
True Fri 8
Accessing rows from dataframe with Boolean index

Clasdf.loc[True] or Clasdf.loc[1]

Days No of classes
True Mon 6
True Wed 3
True Fri 8

You might also like