【发布时间】:2021-07-15 21:55:21
【问题描述】:
我有一个包含一些列的数据框:
>>> np.random.seed(0xFEE7)
>>> df = pd.DataFrame({'A': np.random.randint(10, size=10),
'B': np.random.randint(10, size=10),
'C': np.random.choice(['A', 'B'], size=10)})
>>> df
A B C
0 0 0 B
1 4 0 B
2 6 6 A
3 8 3 B
4 0 2 A
5 8 4 A
6 4 1 B
7 8 7 A
8 4 4 A
9 1 1 A
我还有一个布尔系列匹配df的部分索引:
>>> g = df.groupby('C').get_group('A')
>>> ser = g['B'] > 5
>>> ser
2 True
4 False
5 False
7 True
8 False
9 False
Name: B, dtype: bool
我希望能够使用ser 从df 设置或提取数据。例如:
>>> df.loc[ser, 'A'] -= 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jfoxrabinovitz\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1762, in __getitem__
return self._getitem_tuple(key)
File "C:\Users\jfoxrabinovitz\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1289, in _getitem_tuple
retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
File "C:\Users\jfoxrabinovitz\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1914, in _getitem_axis
return self._getbool_axis(key, axis=axis)
File "C:\Users\jfoxrabinovitz\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1782, in _getbool_axis
key = check_bool_indexer(labels, key)
File "C:\Users\jfoxrabinovitz\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 2317, in check_bool_indexer
raise IndexingError(
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
这个错误是有道理的,因为ser 的长度与df 的长度不同。如何告诉数据框更新与ser 的索引匹配并设置为True 的行?
具体来说,我希望只修改索引 2 和 7 处的条目:
>>> df # after modification
A B C
0 0 0 B
1 4 0 B
2 3 6 A
3 8 3 B
4 0 2 A
5 8 4 A
6 4 1 B
7 5 7 A
8 4 4 A
9 1 1 A
【问题讨论】:
-
您可能需要
df.loc[ser[ser].index,'A']来分配值而不是整个ser.index,因为其他值是假的,不确定预期 - 因此评论:-) -
@anky。感谢您指出了这一点。我已经更新了问题,期待您的回答,因为您的评论正是我想要的。
-
我很难想象上面的Series一开始就无法基于整个DataFrame创建一个类似索引的布尔Series的场景。 IE。以上等价于掩码
df.C.eq('A') & df.B.gt(5)。此外,如果您有重复的索引,使用ser可能会出现问题。 -
@ALollz。请写下来作为答案。我最喜欢的是 XY'd
标签: python pandas indexing mask