gpt4 book ai didi

python - 值错误: cannot reindex from a duplicate axis Error in Pandas

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:50 28 4
gpt4 key购买 nike

我有最后一栏,我正在尝试获取 15 分钟的最大值和最小值。通过执行这段代码。但我无法包含此代码的类型。因为我的类型列中有很多类型。我收到此错误。

Timestamp        Last           Max            Type
1/20/19 12:15 3071.56 Ada
1/20/19 12:17 3097.82 Btc
1/20/19 12:17 3097.82 Ada
1/20/19 12:18 3095.25 Ada
1/20/19 12:19 3087.42 Btc
1/20/19 12:20 3095.29 Btc
1/20/19 12:21 3095.25 Btc
1/20/19 12:22 3093.11 Btc
1/20/19 12:23 3103 Btc
1/20/19 12:24 3095 Btc
1/20/19 12:25 3100.6 Btc
1/20/19 12:26 3099.84 Ada
1/20/19 12:27 3098.77 Ada
1/20/19 12:29 3097.24 Ada
1/20/19 12:29 3090 3103 Ada
1/20/19 12:30 3090 3103 Ada
1/20/19 12:31 3094.29 3103 Ada

我想要一个包含 Ada_max 和 Btc_max 的列

这是我必须计算 15 分钟最后一列最大值的代码。

df['Prev15minMax'] = df['Last'].rolling('15min', min_periods=16).max()
df['Prev15minMin'] = df['Last'].rolling('15min', min_periods=16).min()

尝试过的代码:

for v in df['Type'].unique():
df['Prev15minMax_{v}'] = df.loc[df['Type'].eq(v), 'Last_new'].rolling('15min', min_periods=15).max()

我得到的错误:

ValueError                                Traceback (most recent call last)
<ipython-input-6-43ab5f7d0bc2> in <module>()
1
2 for v in df['Type'].unique():
----> 3 df['Prev60minMax_{v}'] = df.loc[df['Type'].eq(v), 'Last'].rolling('60min', min_periods=60).max()

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
3114 else:
3115 # set column
-> 3116 self._set_item(key, value)
3117
3118 def _setitem_slice(self, key, value):

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _set_item(self, key, value)
3189
3190 self._ensure_valid_index(value)
-> 3191 value = self._sanitize_column(key, value)
3192 NDFrame._set_item(self, key, value)
3193

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _sanitize_column(self, key, value, broadcast)
3361
3362 if isinstance(value, Series):
-> 3363 value = reindexer(value)
3364
3365 elif isinstance(value, DataFrame):

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in reindexer(value)
3353 # duplicate axis
3354 if not value.index.is_unique:
-> 3355 raise e
3356
3357 # other

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in reindexer(value)
3348 # GH 4107
3349 try:
-> 3350 value = value.reindex(self.index)._values
3351 except Exception as e:
3352

/usr/local/lib/python3.6/site-packages/pandas/core/series.py in reindex(self, index, **kwargs)
3320 @Appender(generic._shared_docs['reindex'] % _shared_doc_kwargs)
3321 def reindex(self, index=None, **kwargs):
-> 3322 return super(Series, self).reindex(index=index, **kwargs)
3323
3324 def drop(self, labels=None, axis=0, index=None, columns=None,

/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in reindex(self, *args, **kwargs)
3683 # perform the reindex on the axes
3684 return self._reindex_axes(axes, level, limit, tolerance, method,
-> 3685 fill_value, copy).__finalize__(self)
3686
3687 def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,

/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
3701 obj = obj._reindex_with_indexers({axis: [new_index, indexer]},
3702 fill_value=fill_value,
-> 3703 copy=copy, allow_dups=False)
3704
3705 return obj

/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in _reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
3804 fill_value=fill_value,
3805 allow_dups=allow_dups,
-> 3806 copy=copy)
3807
3808 if copy and new_data is self._data:

/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy)
4412 # some axes don't allow reindexing with dups
4413 if not allow_dups:
-> 4414 self.axes[axis]._can_reindex(indexer)
4415
4416 if axis >= self.ndim:

/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py in _can_reindex(self, indexer)
3557 # trying to reindex on an axis with duplicates
3558 if not self.is_unique and len(indexer):
-> 3559 raise ValueError("cannot reindex from a duplicate axis")
3560
3561 def reindex(self, target, method=None, level=None, limit=None,

ValueError: cannot reindex from a duplicate axis

最佳答案

按两侧的行过滤:

for v in df['Type'].unique():
mask = df['Type'].eq(v)
df.loc[mask, f'Prev15minMax_{v}'] = (df.loc[mask,'Last_new']
.rolling('15min',min_periods=15)
.max())

关于python - 值错误: cannot reindex from a duplicate axis Error in Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55900678/

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