gpt4 book ai didi

Python pandas.core.indexing.IndexingError : Unalignable boolean Series key provided 错误

转载 作者:行者123 更新时间:2023-11-28 21:03:32 25 4
gpt4 key购买 nike

所以我读入了一个包含 29 列的数据表,并添加了一个索引列(总共 30 个)。

Data = pd.read_excel(os.path.join(BaseDir, 'test.xlsx'))
Data.reset_index(inplace=True)

然后,我想对数据进行子集化以仅包括其列名包含“ref”或“Ref”的列;我从另一篇 Stack 帖子中获得了以下代码:

col_keep = Data.ix[:, pd.Series(Data.columns.values).str.contains('ref', case=False)]

但是,我不断收到此错误:

    print(len(Data.columns.values))
30
print(pd.Series(Data.columns.values).str.contains('ref', case=False))
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 False
15 False
16 False
17 False
18 False
19 False
20 False
21 False
22 False
23 False
24 True
25 True
26 True
27 True
28 False
29 False
dtype: bool

Traceback (most recent call last):
File "C:/Users/lala.py", line 26, in <module>
col_keep = FedexData.ix[:, pd.Series(FedexData.columns.values).str.contains('ref', case=False)]
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 84, in __getitem__
return self._getitem_tuple(key)
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 816, in _getitem_tuple
retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1014, in _getitem_axis
return self._getitem_iterable(key, axis=axis)
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1041, in _getitem_iterable
key = check_bool_indexer(labels, key)
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1817, in check_bool_indexer
raise IndexingError('Unalignable boolean Series key provided')
pandas.core.indexing.IndexingError: Unalignable boolean Series key provided

所以 bool 值是正确的,但为什么它不起作用?为什么错误不断弹出?

感谢任何帮助/提示!非常感谢。

最佳答案

我可以通过这种方式重现类似的错误消息:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(4, size=(10,4)), columns=list('ABCD'))
df.ix[:, pd.Series([True,False,True,False])]

加注(使用 Pandas 版本 0.21.0.dev+25.g50e95e0)

pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

出现问题是因为 Pandas 试图对齐 Series 的索引在使用 Series bool 值屏蔽之前使用 DataFrame 的列索引值。因为 df 有列标签 'A', 'B', 'C', 'D' 并且 Series 有索引标签 0123,Pandas 提示标签是不可对齐。

您可能不需要任何索引对齐。因此,传递一个 NumPy bool 数组而不是 Pandas 系列:

mask = pd.Series(Data.columns.values).str.contains('ref', case=False).values
col_keep = Data.loc[:, mask]

Series.values 属性返回一个 NumPy 数组。因为在 Pandas 的 future 版本中,DataFrame.ix will be removed ,在这里使用 Data.loc 而不是 Data.ix 因为我们需要 bool 索引。

关于Python pandas.core.indexing.IndexingError : Unalignable boolean Series key provided 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46374860/

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