Getting last value of dataframe column without index

Suppose I have a DataFrame, for example:

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

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

 df['e'].tail(1) 

but that will return a series with index 9. Ideally, I just want to get the value as a number that I can work directly with. I could also do:

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

but then I will need to access / call the 0th element before I can work with it. Is there a more direct / easy way to do this?

+5
source share
1 answer

You can try iloc method:

 In [26]: df Out[26]: abcde 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 a scalar, you can use iat , which is faster. From docs :

If you want to access a scalar value, the fastest way is to use the at and iat that are implemented in all 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 
+14
source

Source: https://habr.com/ru/post/1237689/


All Articles