williamfaith
williamfaith

Reputation: 277

How to get the last row value in pandas through dataframe.get_value()

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

Answers (4)

rachwa
rachwa

Reputation: 2300

You can also use take:

df['Salary'].take([-1])

Upvotes: 0

Ottotos
Ottotos

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

alanindublin
alanindublin

Reputation: 101

This will select salary in the last row.

df['Salary'][-1]

Upvotes: 4

fklement
fklement

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)


Or you could simply do:

>>>df[-1:]
    animal
8   zebra

Upvotes: 1

Related Questions