Python: Get last row from Pandas Data Frame
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
You have to specifies the number of rows you want to get. So for your case it would be data.tail(1)
to print only the last row. The default value is to print 5 rows, that's why you see your full dataframe with 2 rows.