gpt4 book ai didi

python - 如何克服 'ValueError: Wrong number of items passed 2, placement implies 1'错误?

转载 作者:行者123 更新时间:2023-12-01 07:52:03 25 4
gpt4 key购买 nike

我有一个时间序列 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]

获取列并遵循我正在做的事情:

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

为了克服这个错误需要改变什么?

谢谢!

最佳答案

假设您的“日期”和“时间”列都是 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')

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

关于python - 如何克服 'ValueError: Wrong number of items passed 2, placement implies 1'错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56158499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com