Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
59 views
in Technique[技术] by (71.8m points)

python - 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 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 a more direct/easy way to do this?

question from:https://stackoverflow.com/questions/34166030/obtaining-last-value-of-dataframe-column-without-index

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...