Michael Roberts Michael Roberts - 11 days ago 6
Python Question

Selecting a specific row and column within pandas data array

Hopefully this is a quick and simple one. Having looked through the entirity of the pandas.DataFrame documentation I can't quite seem to find exactly what I need. I have a DataFrame as:

StartDate End Date
0 10/11/2014 00:00 10/12/2014 12:00
1 12/01/2015 08:00 31/01/2015 07:59
2 15/01/2015 00:00 19/02/2015 15:43


Say, for example, I want to access the value at
StartDate
column index value
2
...i.e,
15/01/2015 00:00
how is this achieved?

Answer

Use loc:

print (df.loc[2, 'StartDate'])
15/01/2015 00:00

or faster at:

print (df.at[2, 'StartDate'])
15/01/2015 00:00

In [199]: %timeit (df.loc[2, 'StartDate'])
10000 loops, best of 3: 147 µs per loop

In [200]: %timeit (df.at[2, 'StartDate'])
100000 loops, best of 3: 6.3 µs per loop
Comments