obtaining last value of dataframe column without index

Suppose I have a DataFrame such as:

df = pd.DataFrame(np.random.randn(10,5), columns = ['a','b','c','d','e'])

and I would like to retrieve the last value in column e. I could do:

df['e'].tail(1)

but this would return a series which has index 9 with it. Ideally, I just want to obtain the value as a number that I can work with directly. I could also do:

np.array(df['e'].tail(1))

but this would then require me to access/call the 0'th element of it before I can really work with it. Is there a more direct/easy way to do this?


Solution 1:

You could try iloc method of dataframe:

In [26]: df
Out[26]: 
          a         b         c         d         e
0 -1.079547 -0.722903  0.457495 -0.687271 -0.787058
1  1.326133  1.359255 -0.964076 -1.280502  1.460792
2  0.479599 -1.465210 -0.058247 -0.984733 -0.348068
3 -0.608238 -1.238068 -0.126889  0.572662 -1.489641
4 -1.533707 -0.218298 -0.877619  0.679370  0.485987
5 -0.864651 -0.180165 -0.528939  0.270885  1.313946
6  0.747612 -1.206509  0.616815 -1.758354 -0.158203
7 -2.309582 -0.739730 -0.004303  0.125640 -0.973230
8  1.735822 -0.750698  1.225104  0.431583 -1.483274
9 -0.374557 -1.132354  0.875028  0.032615 -1.131971

In [27]: df['e'].iloc[-1]
Out[27]: -1.1319705662711321

Or if you want just scalar you could use iat which is faster. From docs:

If you only want to access a scalar value, the fastest way is to use the at and iat methods, which are implemented on all of the data structures

In [28]: df.e.iat[-1]
Out[28]: -1.1319705662711321

Benchmarking:

In [31]: %timeit df.e.iat[-1]
100000 loops, best of 3: 18 µs per loop

In [32]: %timeit df.e.iloc[-1]
10000 loops, best of 3: 24 µs per loop

Solution 2:

Try

df['e'].iloc[[-1]]

Sometimes,

df['e'].iloc[-1]

doesn't work.