【问题标题】:Value error in plotting a time series data绘制时间序列数据时的值错误
【发布时间】:2020-04-19 20:03:49
【问题描述】:

我正在尝试为时间序列数据制作条形图/计数图:

avg_curr1 = df[df['name'] == 'name1'].groupby(["Week of the Month", "Month"]).count()
print(avg_curr1)

输出:

Week of the Month  Month  col1  col2  col3  col3  col4
1                  10     1055  1055  1055  1055  1055
                   11     842   842   842   842   842
                   12     789   789   789   789   789
2                  10     668   668   668   668   668
                   11     846   846   846   846   846
                   12     802   802   802   802   802
3                  10     752   752   752   752   752
                   11     684   684   684   684   684
                   12     134   134   134   134   134
4                  10     447   447   447   447   447
                   11     462   462   462   462   462
                   12     386   386   386   386   386
5                  9      58    58    58    58    58
                   10     265   265   265   265   265
                   11     140   140   140   140   140
                   12     230                     

所以基本上,这些是过去三个月的计数值,已分组到每个星期。

然后我尝试了:

sns.set(style="darkgrid")
ax = sns.countplot(x="avg_curr1", data=avg_curr1, hue = 'Month')

这引发了一个值错误。

我是否错误地汇总了数据?在此先感谢您的任何建议!

【问题讨论】:

    标签: python matplotlib data-visualization seaborn


    【解决方案1】:

    修改图表得到输出:

    avg_curr1 = df[df['name'] == 'name1'].groupby(["Week of the Month", "Month"]).count().reset_index().sort_values(by = "name",ascending=False)
    data = pd.concat([avg_curr1], axis=0)
    ax = sns.pointplot("Week of the Month", "name", data = data, hue = "Month", markers = "H")
    

    【讨论】:

      【解决方案2】:

      如果您想分析每月每周的计数,那么您应该使用 barplot 而不是 countplot。

      import pandas as pd
      import seaborn as sns
      avg_curr1=pd.read_excel(INPATH)
      avg_curr1=avg_curr1.ffill(axis = 0)
      sns.set(style="darkgrid")
      s=sns.barplot(x='Week of the Month',y='col1',data=avg_curr1, hue ='Month')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-03
        • 2020-04-09
        • 2013-12-25
        • 2019-08-29
        • 1970-01-01
        • 2018-11-11
        • 1970-01-01
        相关资源
        最近更新 更多