【问题标题】:plot data from a pandas dataframe in a line graph在折线图中绘制来自 pandas 数据框的数据
【发布时间】:2019-03-30 03:31:26
【问题描述】:

我在熊猫中有以下数据框。 date 是两个不同的日期,在本例中为 16 日和 17 日 时间将从 00:00 到 24:00 之后是第二天 :-) 百分比将从 100 变为 0。

    date        time      percentage
   2018-08-16  00:00      36   
   2018-08-16  00:01      37   
   2018-08-16  00:01      38   
   2018-08-16  00:03      39   
   2018-08-16  00:03      40   
   2018-08-16  00:04      41   
   2018-08-16  00:05      42   
   2018-08-16  00:05      43   
   2018-08-16  00:06      44   
   2018-08-16  00:07      45   
   2018-08-16  00:07      46   
   2018-08-16  00:08      47
...
   2018-08-17  07:24      95   
   2018-08-17  07:25      94   
   2018-08-17  07:25      94   
   2018-08-17  07:32      95   
   2018-08-17  07:43      96   
   2018-08-17  07:52      97  
...

现在我想将这个数据绘制成这样的折线图:

尝试过:

df.set_index('date', inplace=True)
df.groupby('time')['percentage'].plot(legend=True)

plt.show()

但总是返回“TypeError: Empty 'DataFrame': no numeric data to plot”

有人可以帮帮我吗?

【问题讨论】:

  • “百分比”的数据类型是什么? df['percentage'] = pd.to_numeric(df['percentage'])

标签: python pandas


【解决方案1】:

你可以试试这个:

df.set_index([df.groupby(['date','time']).cumcount(), 'date', 'time'])['percentage']\
  .unstack('date').reset_index(0, drop=True).sort_index().plot()

【讨论】:

    【解决方案2】:

    应该这样做:

    df.percentage=df.percentage.astype(int)
    for i in df.date.unique():
        aux=df[df.date==i]
        plt.plot(aux.time,aux.percentage)
    plt.show()
    

    【讨论】:

    • 仍然收到错误“TypeError: Empty 'DataFrame': no numeric data to plot”
    • 可能百分比不是数字。你能检查它是否显示为“30%”而不是 30 或 0.3 吗?
    • 如果我打印日期帧,它们就像 36、37、38、39 等,当使用“df = df.convert_objects(convert_numeric=True)”时它可以工作,但它带有警告“FutureWarning: convert_objects 已弃用"
    • 试试我用编辑后的版本发布的内容,告诉我它是否有效,请
    • 太棒了!一个问题是可以在 x-as 上每小时设置一个 tomestamp,此时它显示“时间”想每小时获取它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2018-03-19
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多