Python 熊猫:序列长度不等的布尔索引
给定两个熊猫系列对象A和匹配项。Matches包含的索引子集具有布尔项。如何做逻辑索引的等价物 如果匹配的长度与A相同,则可以使用:Python 熊猫:序列长度不等的布尔索引,python,pandas,Python,Pandas,给定两个熊猫系列对象A和匹配项。Matches包含的索引子集具有布尔项。如何做逻辑索引的等价物 如果匹配的长度与A相同,则可以使用: A[Matches] = 5.*Matches 如果匹配项短于1,则会得到: error: Unalignable boolean Series key provided 编辑1:根据要求编辑插图 火柴系列的构造是人为的,仅供说明。另外,在我的例子中,行索引显然是非数字的,并且不等于元素值…好吧,你不能得到你想要的,因为int64对于一个不包含任何元素的序列
A[Matches] = 5.*Matches
如果匹配项短于1,则会得到:
error: Unalignable boolean Series key provided
编辑1:根据要求编辑插图
火柴系列的构造是人为的,仅供说明。另外,在我的例子中,行索引显然是非数字的,并且不等于元素值…好吧,你不能得到你想要的,因为
int64
对于一个不包含任何元素的序列来说是不可能的数据类型。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
>A=pd.系列(范围(10))
>>>Matches=(A>>A[Matches[Matches].index]=无
>>>A
0无
1无
2无
3 3
4 4
5 5
6 6
7 7
8 8
9 9
数据类型:对象
这是因为
匹配[Matches]
选择了匹配的元素,这些元素是正确的。您能给出一个输入和输出示例吗?我不完全确定您想要什么。@DSM示例添加到问题中。
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
>>> 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