Reputation: 277
I follow this instruction https://www.geeksforgeeks.org/python-pandas-dataframe-get_value/ and know how to get the value from a dateframe data in pandas:
df.get_value(10, 'Salary')
My question is how to get the value of 'Salary' in the last row?
Upvotes: 8
Views: 37234
Reputation: 673
First of all I would advise against using get_value since it is/will be deprecated. (see: https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.get_value.html )
There are a couple of solutions:
df['Salary'].iloc[-1]
df.Salary.iloc[-1]
are synonymous. Iloc is the way to retrieve items in a pandas df by their index.
df['Salary'].values[-1]
creates a list of the Salary column and returns the last items
df['Salary'].tail(1)
df.Salary.tail(1)
returns the last row of the salary column.
Which of the solutions is best, depends on the context and your personal preference.
Upvotes: 22
Reputation: 61
In pandas there exists a tail function:
>>> df = pd.DataFrame({'animal': ['alligator', 'bee', 'falcon', 'lion',
'monkey', 'parrot', 'shark', 'whale', 'zebra']})
>>> df.tail(1)
animal
8 zebra
(See: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.tail.html)
>>>df[-1:]
animal
8 zebra
Upvotes: 1