Juliana Rivera Juliana Rivera - 1 month ago 4
Python Question

Get the value of a cell by row and column

CD_FARE MTH DAY ID_CALENDAR H0 H1 H2 H3 PE1 PE2 PE3 PE4
2.0 1 M Cal01 1 2 1 3 0.14 0.15 0.1 0.2
2.0 1 T Cal01 1 2 1 3 0.14 0.16 0.1 0.2
2.0 1 W Cal01 1 2 4 3 0.14 0.12 0.1 0.2
2.0 1 T Cal01 2 2 1 3 0.14 0.11* 0.1 0.2
2.0 1 F Cal01 4 2 1 3 0.14 0.18 0.1 0.2


I want to know how can I get the value from a specific cell.

For example: I want to return the value 0.11.
I know the position of the row (In this case 3), and the name of the column (PE2).
Can I select the data in this way?:

data = df.iloc[3, 'PE2']

Answer

Obviously it does not work, it gives a ValueError

ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

But if you use df.loc[3, 'PE2'] instead of the iloc method, it works