Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Get last row from Pandas Data Frame

Tags:

python

pandas

I'm trying to use some financial functions written in Python which return a Panda Data Frame.

The following function returns a Panda Data Frame:

from yahoo_fin import stock_info as si

data = si.get_data("ENEL.MI", start_date="01/21/2022 8:00", end_date="01/21/2022 16:30",index_as_date=False, interval="1d")

Here is what I get if I print data:

        date   open   high    low  close  adjclose    volume   ticker
0 2022-01-21  6.976  6.993  6.855  6.905     6.905  33639775  ENEL.MI
1 2022-01-21  6.976  6.993  6.855  6.905     6.905  35419140  ENEL.MI

I'd like to collect just the last row from the DataFrame (the row with number "1") So, I've tried with:

lastrow = data.tail()
print(lastrow)

However I still get the same result (the whole DataFrame is printed). I'm a bit puzzled. Is there a way to get just the last row? Thanks a lot

like image 932
Carla Avatar asked Mar 20 '26 01:03

Carla


1 Answers

With iloc() function, we can retrieve a particular value belonging to a row and column using the index values assigned to it.

lastrow = data.iloc[-1]
like image 163
Avatazjoe Avatar answered Mar 22 '26 13:03

Avatazjoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!