【问题标题】:Plot a data frame绘制数据框
【发布时间】:2016-01-31 12:31:15
【问题描述】:

我有一个这样的数据框:

ReviewDate_month,ProductId,Reviewer
01,185,185
02,155,155
03,130,130
04,111,111
05,110,110
06,98,98
07,101,92
08,71,71
09,73,73
10,76,76
11,105,105
12,189,189

我想绘制它,X 中的 ReviewDate_Month,Y 中的产品 ID 和审阅者。但我将从 Product ID 或 Reviewer 的 1 行开始。 所以我尝试了:

df_no_monthlycount.plot.line

收到以下错误消息:

File "C:/Users/user/PycharmProjects/Assign2/Main.py", line 59, in <module>
01                      185       185
02                      155       155
03                      130       130
04                      111       111
05                      110       110
06                       98        98
07                      101        92
08                       71        71
09                       73        73
10                       76        76
    df_no_monthlycount.plot.line
AttributeError: 'function' object has no attribute 'line'
11                      105       105
12                      189       189

Process finished with exit code 1

我也试过这个:

df_no_monthlycount.plot(x=df_helful_monthlymean['ReviewDate_month'],y=df_helful_monthlymean['ProductId'],style='o')

这样的错误信息:

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Assign2/Main.py", line 52, in <module>
    df_no_monthlycount.plot(x=df_helful_monthlymean['ReviewDate_month'],y=df_helful_monthlymean['ProductId'],style='o')
  File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1797, in __getitem__
    return self._getitem_column(key)
  File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1804, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 2851, in get
    loc = self.items.get_loc(item)
  File "C:\Python34\lib\site-packages\pandas\core\index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3838)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3718)
  File "pandas\hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12294)
  File "pandas\hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12245)
KeyError: 'ReviewDate_month'

【问题讨论】:

标签: python pandas plot


【解决方案1】:

如果你想在XProduct IDReviewer 中绘制ReviewDate_MonthY,你可以这样做:

df_no_monthlycount.plot(x='ReviewDate_Month', y=['Product ID', 'Reviewer'])

【讨论】:

  • 也许我的问题有点误导。最好的是我可以绘制两条线,但我现在遇到的问题是我什至无法绘制一条线。我尝试了我自己的第二种方法,我得到了那些关键错误消息。
  • @Jasmine 我的答案中的代码将为您提供plot 2 行:Product IDReviewer
  • 它会抛出与我使用的相同的错误消息 - df_no_monthlycount.plot(x=df_helful_monthlymean['ReviewDate_month'],y=df_helful_monthlymean['ProductId'],style='o')
【解决方案2】:

拨打plot如下图:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')

print(df)

df.plot(x ='ReviewDate_month',y=['ProductId', 'Reviewer'] ,kind='line')
plt.show()

会给你:

【讨论】:

  • 事实上,如果我将文件保存到 csv,并将 csv 读回数据帧,那么您的代码就可以工作。但是,我的数据框实际上来自 groupby "df_monthlycount = df_full[['ProductId','Reviewer','ReviewDate_month']].groupby(['ReviewDate_month']).count()" 情节不适用于那,我的选项2中提到的错误消息,有什么想法吗?
  • df_monthlycount 是一个数据框。你应该可以做到df_montlycount.plot(x ='ReviewDate_month',y=['ProductId', 'Reviewer'] ,kind='line') 那也会报错?
  • 你可以自己检查,做type(df_monthlycount)
  • 是的。 我注意到的一件事是月份列是 str,保存到 CSV 后,它变成了数字。
  • 那有没有用,只是在做df_montlycount.plot(x ='ReviewDate_month',y=['ProductId', 'Reviewer'] ,kind='line')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 2020-08-23
相关资源
最近更新 更多