Juliana Rivera
Juliana Rivera

Reputation: 1083

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']

Upvotes: 2

Views: 607

Answers (2)

jezrael
jezrael

Reputation: 862396

If need select by position need Series.iloc:

print (df['PE2'].iloc[3])
0.11

Sample:

df = pd.DataFrame({'PE2':[1,2,3],
                   'B':[4,5,6]}, index=['a','b','c'])

print (df)
   B  PE2
a  4    1
b  5    2
c  6    3

#third row in colum PE2
print (df['PE2'].iloc[2])
3

#index value c and column PE2
print (df.ix['c','PE2'])
3

#index value c and column PE2
print (df.loc['c','PE2'])
3

#third row and second column
print (df.iloc[2,1])
3

but if need select by index and column value use ix or DataFrame.loc:

df = pd.DataFrame({'PE2':[1,2,3],
                   'B':[4,5,6]})

print (df)
   B  PE2
0  4    1
1  5    2
2  6    3

print (df.loc[2, 'PE2'])
3

print (df.ix[2, 'PE2'])
3

You can also check selection by label and selection by position in pandas documentation.

Upvotes: 0

firelynx
firelynx

Reputation: 32194

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

Upvotes: 2

Related Questions