【问题标题】:How do i sort month and year in pandas for timeseries visualization?如何在熊猫中对月份和年份进行排序以进行时间序列可视化?
【发布时间】:2021-11-26 02:44:04
【问题描述】:
#plot data
fig, ax = plt.subplots(figsize=(25,17))

plt.ylabel('No of tweets', fontsize=12)
#plt.xlim([1,20])
plt.title('Number of tweets', fontsize = 20)
data.sort_values(by = ['Year','Month'], ascending=[True,True]).groupby(['Month','Year']).count()['text'].plot(ax=ax)
plt.xlabel('Month-Year', fontsize=12)

I have attached the current output here

你能帮我理解我做错了什么吗?

【问题讨论】:

    标签: pandas sorting plot group-by time-series


    【解决方案1】:

    YearMonth 列合并到一个新列Date

    data['Date'] = data['Year'].astype('str') + '-' \
                   + data['Month'].astype('str').str.zfill(2)
    
    # Groupby sort groups by default
    data.groupby('Date')['text'].count().plot(ax=ax)
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2021-08-08
      • 2018-11-24
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      相关资源
      最近更新 更多