Python ValueError:无法从中的重复轴错误重新索引
我有最后一个专栏,我试图得到15分钟的最大值和最小值。通过执行此代码。但我无法包含此代码的类型。因为我在Type列中有很多类型。我得到这个错误Python ValueError:无法从中的重复轴错误重新索引,python,pandas,Python,Pandas,我有最后一个专栏,我试图得到15分钟的最大值和最小值。通过执行此代码。但我无法包含此代码的类型。因为我在Type列中有很多类型。我得到这个错误 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
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
ValueError回溯(最近一次调用)
在()
1.
2表示df['Type']中的v。唯一()
---->3 df['Prev60minMax{v}']=df.loc[df['Type'].eq(v),'Last'].滚动('60min',最小周期=60).max()
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in_u___设置项__(self、key、value)
3114其他:
3115#设置列
->3116自设置项(键、值)
3117
3118定义设置项切片(自身、键、值):
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in_set_项(self、key、value)
3189
3190自我确保有效索引(值)
->3191 value=self.\u sanitize\u列(键,值)
3192 NDFrame.\u设置\u项(自身、键、值)
3193
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py在_sanitize_列中(self、key、value、broadcast)
3361
3362如果存在(值,系列):
->3363值=重新索引器(值)
3364
3365 elif isinstance(值,数据帧):
/reindexer中的usr/local/lib/python3.6/site-packages/pandas/core/frame.py(值)
3353#复制轴
3354如果value.index.is_不是唯一的:
->3355 e
3356
3357#其他
/reindexer中的usr/local/lib/python3.6/site-packages/pandas/core/frame.py(值)
3348#GH 4107
3349请尝试:
->3350值=value.reindex(自索引)。\u值
3351例外情况除外,如e:
3352
/reindex中的usr/local/lib/python3.6/site-packages/pandas/core/series.py(self,index,**kwargs)
3320@Appender(通用._shared_docs['reindex']%_shared_doc_kwargs)
3321 def重新索引(自身,索引=无,**kwargs):
->3322返回超级(系列,自).reindex(索引=索引,**kwargs)
3323
3324 def下降(自身,标签=无,轴=0,索引=无,列=无,
/reindex中的usr/local/lib/python3.6/site-packages/pandas/core/generic.py(self,*args,**kwargs)
3683#在轴上执行重新索引
3684返回自重新索引轴(轴、水平、极限、公差、方法、,
->3685填写值,复制)。\uuuuuu完成\uuuuuuuuuu(自我)
3686
3687定义重新索引轴(自身、轴、水平、限制、公差、方法、填充值、,
/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in_reindex_轴(自身、轴、水平、限制、公差、方法、填充值、副本)
3701 obj=obj.\u用索引器({axis:[new\u index,indexer]}重新索引\u,
3702填充值=填充值,
->3703复制=复制,允许重复(错误)
3704
3705返回obj
/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in_reindex_与索引器(self、reindexer、fill_value、copy、allow_dups)
3804填充值=填充值,
3805允许重复=允许重复,
->3806份=份)
3807
3808如果复制和新建数据为自身数据:
/reindex\u索引器中的usr/local/lib/python3.6/site-packages/pandas/core/internals.py(self、new\u轴、索引器、轴、填充值、允许复制)
4412#某些轴不允许使用DUP重新编制索引
4413如果不允许重复:
->4414自身轴[轴]。\u可以\u重新索引(索引器)
4415
4416如果轴>=self.ndim:
/usr/local/lib/python3.6/site-packages/pandas/core/index/base.py in_can_reindex(self,indexer)
3557#尝试在具有重复项的轴上重新编制索引
3558如果不是self.is_unique和len(索引器):
->3559提升值错误(“无法从重复轴重新编制索引”)
3560
3561 def重新索引(自身、目标、方法=无、级别=无、限制=无、,
ValueError:无法从重复轴重新编制索引
两侧按行安装:
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())
两侧成排安装:
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())
感谢救生员:)感谢救生员:)