Pandas:具有不等系列长度的布尔索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23088921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: boolean indexing with unequal Series lengths
提问by ARF
Given two pandas series objects A and Matches. Matches contains a subset of the indexes of A and has boolean entries. How does one do the equivalent of logical indexing?
给定两个 Pandas 系列对象 A 和 Matches。Matches 包含 A 的索引的子集并具有布尔条目。如何做相当于逻辑索引?
If Matches were the same length as A, one could just use:
如果匹配的长度与 A 相同,则可以使用:
A[Matches] = 5.*Matches
With Matches shorter than A one gets:
比 A 短的比赛得到:
error: Unalignable boolean Series key provided
Edit 1: Illustration as requested
编辑 1:按要求插图
In [15]: A = pd.Series(range(10))
In [16]: A
Out[16]: 0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int64
In [17]: Matches = (A<3)[:5]
In [18]: Matches
Out[18]: 0 True
1 True
2 True
3 False
4 False
dtype: bool
In [19]: A[Matches] = None
---------------------------------------------------------------------------
IndexingError Traceback (most recent call last)
<ipython-input-19-7a04f32ce860> in <module>()
----> 1 A[Matches] = None
C:\Anaconda\lib\site-packages\pandas\core\series.py in __setitem__(self, key, value)
631
632 if _is_bool_indexer(key):
--> 633 key = _check_bool_indexer(self.index, key)
634 try:
635 self.where(~key, value, inplace=True)
C:\Anaconda\lib\site-packages\pandas\core\indexing.py in _check_bool_indexer(ax, key)
1379 mask = com.isnull(result.values)
1380 if mask.any():
-> 1381 raise IndexingError('Unalignable boolean Series key provided')
1382
1383 result = result.astype(bool).values
IndexingError: Unalignable boolean Series key provided
In [20]:
The result I am looking for is:
我正在寻找的结果是:
In [16]: A
Out[16]: 0 None
1 None
2 None
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int64
The construction of the Matches series is artificial and for illustration only. Also, in my case row indexes are obviously non-numeric and not equal to element values...
Matches 系列的构建是人为的,仅用于说明。此外,在我的情况下,行索引显然是非数字的,不等于元素值......
采纳答案by DSM
Well, you can't have what you want, because int64is not a possible dtype for a series containing None. None isn't an integer. But you can get close:
好吧,你不能拥有你想要的东西,因为int64对于包含 None 的系列来说,这不是一个可能的 dtype。None 不是整数。但你可以接近:
>>> A = pd.Series(range(10))
>>> Matches = (A<3)[:5]
>>> A[Matches[Matches].index] = None
>>> A
0 None
1 None
2 None
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: object
Which works because Matches[Matches]selects the elements of Matcheswhich are true.
之所以有效,是因为Matches[Matches]选择了Matches其中正确的元素。

