【问题标题】:getting error with plot with panda DataFrame使用 panda DataFrame 绘制错误
【发布时间】:2018-01-09 10:47:36
【问题描述】:

我正在尝试根据该数据创建图

   Word    count
17  in      2
15  on      2
33  the     2
8   you     2
0   valar   1
29  all     1
24  +       1
25  hotel   1
26  are     1

使用此代码:

%matplotlib inline   
words_df[words_df['count'] > 10000].plot(x='Word',kind='bar')

但我收到此消息错误:

TypeError: Empty 'DataFrame': no numeric data to plot

你能告诉我问题出在哪里吗???

【问题讨论】:

  • 你的数据框是什么样子的?你能发布print(words_df.head())的输出吗?
  • 它看起来像我在帖子中发布的内容。字数 17 in 2 15 on 2 33 the 2 8 you 2 0 valar 1 29 all 1 24 + 1 25 hotel 1 26 are 1

标签: python pandas dataframe matplotlib plot


【解决方案1】:

试试这个:

df[df['count'] > 1000].set_index('Word').plot.bar(rot=0)    

演示(使用您的示例数据集):

In [184]: df
Out[184]:
     Word  count
17     in      2
15     on      2
33    the      2
8     you      2
0   valar      1
29    all      1
24      +      1
25  hotel      1
26    are      1

In [185]: df.dtypes
Out[185]:
Word     object
count     int64   # <--------- NOTE !
dtype: object

In [187]: df[df['count'] > 1].set_index('Word').plot.bar(rot=0, grid=True)
Out[187]: <matplotlib.axes._subplots.AxesSubplot at 0xd608ac8>

结果:

【讨论】:

  • 我试过了,但它给了我这个错误:TypeError: Empty 'DataFrame': no numeric data to plot
  • @yok,这意味着您的 count 列具有非数字 dtype。 print(df.dtypes) 输出什么?
  • Word 对象/count int64 / dtype: object
  • 谢谢@MaxU,它现在运行良好,但我能确切知道是什么问题吗?
  • @yok,正如我之前已经说过的那样,该错误意味着value(Y 轴)列(在您的情况下为count)具有非数字 dtype(对象、日期时间等)。 )
猜你喜欢
  • 2015-08-11
  • 2020-01-21
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
  • 2019-08-09
  • 2018-04-07
相关资源
最近更新 更多