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:
Solution 1:
You could try iloc
method of dataframe:
In[26]: dfOut[26]:
abcde0-1.079547-0.7229030.457495-0.687271-0.78705811.3261331.359255-0.964076-1.2805021.46079220.479599-1.465210-0.058247-0.984733-0.3480683-0.608238-1.238068-0.1268890.572662-1.4896414-1.533707-0.218298-0.8776190.6793700.4859875-0.864651-0.180165-0.5289390.2708851.31394660.747612-1.2065090.616815-1.758354-0.1582037-2.309582-0.739730-0.0043030.125640-0.97323081.735822-0.7506981.2251040.431583-1.4832749-0.374557-1.1323540.8750280.032615-1.131971In[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
andiat
methods, which are implemented on all of the data structures
In[28]: df.e.iat[-1]Out[28]: -1.1319705662711321
Benchmarking:
In[31]: %timeitdf.e.iat[-1]100000loops, bestof3: 18 µsperloopIn[32]: %timeitdf.e.iloc[-1]10000loops, bestof3: 24 µsperloop
Solution 2:
Try
df['e'].iloc[[-1]]
Sometimes,
df['e'].iloc[-1]
doesn't work.
Post a Comment for "Obtaining Last Value Of Dataframe Column Without Index"