Forum

Notifications
Clear all

Intermediate Python - loc  

   RSS

0

I have this data

image

When I am trying to print print(df.loc['Net Amount Due From Branches' ]) there is an error 

 

KeyError: 'the label [Net Amount Due From Branches] is not in the [index]'

@deepika
I suspect this is because there are spaces before 'Net Amount Due From Branches', so it's actually something like ' Net Amount Due From Branches'. You can use another method of printing the row like with iloc.

@Yi Sheng

Thank you. iloc works. 

I tried to remove spaces before lables, but loc did not work. Then I tried to relabel, then the loc worked as below:

df = pd.read_csv("/pythondatafiles/Singapore direct investment abroad1.csv", index_col = 0)

row_lables = ['a','b','c','d','e','f','g']

df.index = row_lables

print(df.loc[['a','b']])

Share:

Delete your account