Pandas:数据框有2列为1

时间:2018-04-23 16:28:49

标签: python-3.x pandas

q.head()

输出

                Weekly_Sales
Date                    
2010-02-28     131963.08
2010-03-31      91237.14
2010-04-30     150516.76
2010-05-31      66694.15
2010-06-30      66740.70

现在我面临的问题是,我想绘制'日期'专栏vs' Weekly_Sales'柱。我已经使用了命令

q=y.resample('M',on='Date').sum()

将每周数据转换为每月数据,从而产生较高的数据帧。

type(q)

输出"类' pandas.core.frame.DataFrame'"显示q是一个数据框。现在,由于q没有如此处所示的两个不同的列,

q.Weekly_Sales

输出

Date
2010-02-28    131963.08
2010-03-31     91237.14
2010-04-30    150516.76
2010-05-31     66694.15
2010-06-30     66740.70
2010-07-31     81915.01
2010-08-31     64578.81
2010-09-30     71913.27
2010-10-31    134644.53
2010-11-30     92161.40
2010-12-31    173983.88
2011-01-31     69146.59
2011-02-28    125762.63
2011-03-31     82823.34
2011-04-30    165056.95
2011-05-31     68251.72
2011-06-30     62978.57
2011-07-31     78856.23
2011-08-31     59061.95
2011-09-30     87756.41
2011-10-31     98806.83
2011-11-30     98537.51
2011-12-31    174512.07
2012-01-31     70205.35
2012-02-29    134683.30
2012-03-31    114680.54
2012-04-30    125600.12
2012-05-31     70792.98
2012-06-30     83646.54
2012-07-31     66468.79
2012-08-31     83045.57
2012-09-30     76137.90
2012-10-31     96244.56
Freq: M, Name: Weekly_Sales, dtype: float64

,而

q.Date

输出

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    q.Date
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\generic.py", line 3614, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'Date'

因为这两列都在q.Weekly_Sales之下,我如何将它们分开以获得2列并最终绘制它们?

1 个答案:

答案 0 :(得分:1)

double [[]]会将单个列查询为数据帧而非系列,然后我们使用reset_index

new_s=q[['Weekly_Sales']].reset_index()