sách gpt4 ai đã đi

python - 使用 GroupBy 获取 Pandas 的平均值 - 获取 DataError : No numeric types to aggregate -

In lại Tác giả: Space Dog 更新时间:2023-10-30 01:58:38 50 4
mua khóa gpt4 Nike

我知道有很多关于此的问题,例如 Getting daily averages with pandasHow get monthly mean in pandas using groupby但我遇到了一个奇怪的错误。

简单的数据集,有一个索引列(时间戳类型)和一个值列。想获得数据的月均值。

In [76]: df.head()
Out[76]:
MỘT
2008-01-02 1
2008-01-03 2
2008-01-04 3
2008-01-07 4
2008-01-08 5

但是,当我进行 groupby 时,我得到的只是索引组而不是值组

In [74]: df.head().groupby(lambda x: x.month).groups
Out[74]:
{1: [Timestamp('2008-01-02 00:00:00'),
Timestamp('2008-01-03 00:00:00'),
Timestamp('2008-01-04 00:00:00'),
Timestamp('2008-01-07 00:00:00'),
Timestamp('2008-01-08 00:00:00')]}

尝试采用 means() 会导致错误:

已经尝试了 df.head().resample("M", how='mean')df.head().groupby(lambda x: x.month) .mean()

并得到错误:DataError: No numeric types to aggregate

In [75]: df.resample("M", how='mean')
---------------------------------------------------------------------------
DataError Traceback (most recent call last)
in ()
----> 1 df.resample("M", how='mean')

/usr/local/lib/python2.7/site-packages/pandas/core/generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base)
2878 fill_method=fill_method, convention=convention,
2879 limit=limit, base=base)
-> 2880 return sampler.resample(self).__finalize__(self)
2881
2882 def first(self, offset):

/usr/local/lib/python2.7/site-packages/pandas/tseries/resample.pyc in resample(self, obj)
82
83 if isinstance(ax, DatetimeIndex):
---> 84 rs = self._resample_timestamps()
85 elif isinstance(ax, PeriodIndex):
86 offset = to_offset(self.freq)

/usr/local/lib/python2.7/site-packages/pandas/tseries/resample.pyc in _resample_timestamps(self)
286 # Irregular data, have to use groupby
287 grouped = obj.groupby(grouper, axis=self.axis)
--> 288 result = grouped.aggregate(self._agg_method)
289
290 if self.fill_method is not None:

/usr/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in aggregate(self, arg, *args, **kwargs)
2436 def aggregate(self, arg, *args, **kwargs):
2437 if isinstance(arg, compat.string_types):
-> 2438 return getattr(self, arg)(*args, **kwargs)
2439
2440 result = OrderedDict()

/usr/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in mean(self)
664 """
665 try:
--> 666 return self._cython_agg_general('mean')
667 except GroupByError:
668 raise

/usr/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in _cython_agg_general(self, how, numeric_only)
2356
2357 def _cython_agg_general(self, how, numeric_only=True):
-> 2358 new_items, new_blocks = self._cython_agg_blocks(how, numeric_only=numeric_only)
2359 return self._wrap_agged_blocks(new_items, new_blocks)
2360

/usr/local/lib/python2.7/site-packages/pandas/core/groupby.pyc in _cython_agg_blocks(self, how, numeric_only)
2406
2407 if len(new_blocks) == 0:
-> 2408 raise DataError('No numeric types to aggregate')
2409
2410 return data.items, new_blocks

DataError: No numeric types to aggregate

1 Câu trả lời

是的,您应该尝试使用 df['A'] = df['A'].astype(int) Sẽ MỘT 强制转换为数字。可能值得检查初始数据读入中是否有任何内容导致它也是对象而不是数字。

关于python - 使用 GroupBy 获取 Pandas 的平均值 - 获取 DataError : No numeric types to aggregate -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397057/

50 4 0
Bài viết được đề xuất: python - babelize_shell() 在 NLTK 包中不起作用
Bài viết được đề xuất: sql - 你怎么称呼你的美国国家表?
Bài viết được đề xuất: sql - TSQL RAND 随机值
Bài viết được đề xuất: python - 如何使用 pandas 包为 python 制作的子图清除 boxplot 的默认字幕
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com