如何克服“ ValueError:传递的项目数错误2,展示位置隐含1”错误?

时间:2019-05-15 22:33:33

标签: python python-3.x pandas dataframe pandas-groupby

我有一个时间序列df:

    Menge     Dates      Time       month
    19.5    2018-01-01  00:00:00     Jan
    19.0    2018-01-01  00:15:00     Jan
    19.5    2018-01-01  00:30:00     Jan
    19.5    2018-01-01  00:45:00     Jan
    21.0    2018-01-01  01:00:00     Jan
    19.5    2018-01-01  01:15:00     Jan
    20.0    2018-01-01  01:30:00     Jan
    23.0    2018-01-01  01:45:00     Jan
    20.5    2018-01-01  02:00:00     Jan
    84.5    2018-01-02  02:00:00     Jan
    80.0    2018-01-02  02:15:00     Jan
    75.5    2018-01-02  02:30:00     Jan
    72.0    2018-01-02  02:45:00     Jan
    70.0    2018-01-02  03:00:00     Jan
    69.0    2018-01-02  03:15:00     Jan
    67.5    2018-01-02  03:30:00     Jan
    67.0    2018-01-02  03:45:00     Jan
    66.0    2018-01-02  04:00:00     Jan
    189.5   2018-02-06  07:00:00     Feb
    188.0   2018-02-06  07:15:00     Feb
    190.5   2018-02-06  07:30:00     Feb
    192.0   2018-02-06  07:45:00     Feb
    185.5   2018-02-06  08:00:00     Feb
    182.5   2018-02-06  08:15:00     Feb
    178.0   2018-02-06  08:30:00     Feb
    189.5   2018-02-06  08:45:00     Feb
    181.0   2018-02-06  09:00:00     Feb
    161.0   2018-02-06  21:00:00     Feb
    159.0   2018-02-06  21:15:00     Feb
    163.5   2018-02-06  21:30:00     Feb
    162.5   2018-02-06  21:45:00     Feb
    163.0   2018-02-06  22:00:00     Feb
    162.5   2018-02-06  22:15:00     Feb
    162.5   2018-02-06  22:30:00     Feb
    162.0   2018-02-06  22:45:00     Feb
    158.5   2018-02-06  23:00:00     Feb

我正在尝试计算相同的每小时,每天和每月平均值。

我已经完成:

data['month'] = [d.strftime('%b') for d in data.Dates]

获取month列以及我正在做的事情:

data_nan_dropped = data.dropna(axis = 0)
data_nan_dropped.Dates = pd.to_datetime(data_nan_dropped.Dates)
data_nan_dropped.Time = pd.to_datetime(data_nan_dropped.Time, format='%H:%M:%S')
hourly_mean = data_nan_dropped.groupby([data_nan_dropped.Dates, data_nan_dropped.Time.dt.hour]).mean()
monthly_mean = data_nan_dropped.groupby(data_nan_dropped.month).mean()
daily_mean = data_nan_dropped.groupby([data_nan_dropped.Dates]).mean()

此代码绝对可以正常工作,但是我想要的是将这些每小时,每月,每天的均值列添加到我的数据框data_nan_dropped中,为此,我尝试将上述代码编辑为:

data_nan_dropped['hourly_mean'] = data_nan_dropped.groupby([data_nan_dropped.Dates, data_nan_dropped.Time.dt.hour]).transform('mean')
data_nan_dropped['monthly_mean'] = data_nan_dropped.groupby(data_nan_dropped.month).transform('mean')
data_nan_dropped['daily_mean'] = data_nan_dropped.groupby([data_nan_dropped.Dates]).transform('mean')

data_nan_dropped['hourly_mean']可以正常工作,并且在我的数据框中创建了一个名为hourly_mean的新列。

但是对于monthly_meandaily_mean,出现以下错误:

Traceback (most recent call last):

  File "<ipython-input-5-159d11ea8819>", line 1, in <module>
    data_nan_dropped['daily_mean'] = data_nan_dropped.groupby([data_nan_dropped.Dates]).transform('mean')

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\frame.py", line 3370, in __setitem__
    self._set_item(key, value)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\frame.py", line 3446, in _set_item
    NDFrame._set_item(self, key, value)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\generic.py", line 3172, in _set_item
    self._data.set(key, value)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\internals\managers.py", line 1056, in set
    self.insert(len(self.items), item, value)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\internals\managers.py", line 1158, in insert
    placement=slice(loc, loc + 1))

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\internals\blocks.py", line 3095, in make_block
    return klass(values, ndim=ndim, placement=placement)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\internals\blocks.py", line 87, in __init__
    '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))

ValueError: Wrong number of items passed 2, placement implies 1

要克服此错误必须更改什么?

谢谢!

3 个答案:

答案 0 :(得分:1)

鉴于您的“日期”和“时间”列均为datetime64 [ns],则:

df = data.dropna(axis = 0)
df['Dates'] = pd.to_datetime(df['Dates'])
df['Time'] = pd.to_datetime(df['Time'])

df['month_mean'] = df.groupby(df['Dates'].dt.month).transform('mean')
df['hourbyday_mean'] =df.groupby([df['Dates'].dt.day, df['Time'].dt.hour]).transform('mean')
df['day_mean'] =df.groupby(df['Dates'].dt.day).transform('mean')

将为您提供三个新列,其各自的均值。

答案 1 :(得分:0)

这里的问题是您应该指出获取mean的列,这里我假设它是Menge

data_nan_dropped['hourly_mean'] = data_nan_dropped.groupby([data_nan_dropped.Dates, data_nan_dropped.Time.dt.hour])['Menge'].transform('mean')

答案 2 :(得分:0)

我再次使用该代码,下面的代码为我提供了正确的答案:

data_nan_dropped.Dates = pd.to_datetime(data_nan_dropped.Dates)
data_nan_dropped.Time = pd.to_datetime(data_nan_dropped.Time, format='%H:%M:%S')
data_nan_dropped['monthly_mean'] = data_nan_dropped.groupby(data_nan_dropped['month'])['Menge'].transform('mean')
data_nan_dropped['hourly_mean'] = data_nan_dropped.groupby([data_nan_dropped['Dates'], data_nan_dropped['Time'].dt.hour])['Menge'].transform('mean')
data_nan_dropped['daily_mean'] =data_nan_dropped.groupby(data_nan_dropped['Dates'])['Menge'].transform('mean')