Python and Pandas - Sorting by Date

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-20 09:00:56

问题


What I've tried for a sort "should" be working; but it is not.

I've queried the "Alpha Vantage" API using the "alpha_vantage" Python library. Below is my code. I am requesting to sort by date; but, as you can see from the output in the df.head() the sorting by date is in the wrong direction. However, the plot is going in the right direction.

df, meta_data = ts.get_daily(symbol='AAPL',outputsize=365)
df['4. close'].plot()
plt.title('Intraday Times Series for the AAPL stock (5 min)')

# Attempting sort here!  Also I've tried df.sort_values(by=['date'])
df.sort_values(by=['date'])  
print(plt.show())
print(df.head())

Output:

None
date         1. open  2. high  3. low  4. close   5. volume

2019-11-13   261.13   262.95  261.07  262.4307   3284781.0

2019-11-12   261.55   262.79  260.92  261.9600  21826100.0

2019-11-11   258.30   262.47  258.28  262.2000  20455300.0

2019-11-08   258.69   260.44  256.85  260.1400  17496600.0

2019-11-07   258.74   260.35  258.11  259.4300  23735100.0

回答1:


df = df.sort_values(by=['date'])

should get it done



来源:https://stackoverflow.com/questions/58840655/python-and-pandas-sorting-by-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!