I have two Series of different lengths, and I want to get the indices for which both the indices and the amount are the same in both series.
我有两个不同长度的系列,我想得到两个系列中指数和数量都相同的指数。
Here are the Series:
这是系列:
ipdb> s1
s1
000007720 2000.00
group1 -3732.05
group t3 2432.12
group2 -38147.87
FSHLAJ -36711.09
EWkayuwo -3.22
Name: amount, dtype: float64
ipdb> s2
s2
000007720 2000.00
group1 -3732.05
group z 12390.00
group y 68633.43
group x 25.00
group w 3913.00
group v -12750.50
group u -53.49
group t -7500.00
group s -1575.82
group r -10.00
group q 1800.00
group p -4510.34
EWFhjkaQU 455.96
group2 -38147.87
FSHLAJ -36711.09
GEKWJ 5.54
Name: amount, dtype: float64
When I try to compare them, I get:
当我尝试比较它们时,我得到:
ipdb>s1 == s2
*** ValueError: Series lengths must match to compare
How can I achieve my objective?
我怎样才能实现目标?
1 个解决方案
#1
You want to use isin
:
你想使用isin:
In [121]:
s2[s2.isin(s1)]
Out[121]:
000007720
group1 -3732.05
group2 -38147.87
FSHLAJ -36711.09
Name: 2000.00, dtype: float64
I don't know which way round you wanted to perform the comparison, here is the other way:
我不知道你想要进行哪种比较,这是另一种方式:
In [122]:
s1[s1.isin(s2)]
Out[122]:
000007720
group1 -3732.05
group2 -38147.87
FSHLAJ -36711.09
Name: 2000.00, dtype: float64
The problem with trying to do s1 == s2
is that it doesn't make sense comparing Series or arrays of different lengths.
尝试执行s1 == s2的问题在于比较不同长度的Series或数组是没有意义的。
If you want the indices to match also then add this as a condition:
如果您希望索引也匹配,则将其添加为条件:
In [131]:
s1[(s1.index.isin(s2.index)) & (s1.isin(s2))]
Out[131]:
000007720
group1 -3732.05
group2 -38147.87
FSHLAJ -36711.09
Name: 2000.00, dtype: float64
#1
You want to use isin
:
你想使用isin:
In [121]:
s2[s2.isin(s1)]
Out[121]:
000007720
group1 -3732.05
group2 -38147.87
FSHLAJ -36711.09
Name: 2000.00, dtype: float64
I don't know which way round you wanted to perform the comparison, here is the other way:
我不知道你想要进行哪种比较,这是另一种方式:
In [122]:
s1[s1.isin(s2)]
Out[122]:
000007720
group1 -3732.05
group2 -38147.87
FSHLAJ -36711.09
Name: 2000.00, dtype: float64
The problem with trying to do s1 == s2
is that it doesn't make sense comparing Series or arrays of different lengths.
尝试执行s1 == s2的问题在于比较不同长度的Series或数组是没有意义的。
If you want the indices to match also then add this as a condition:
如果您希望索引也匹配,则将其添加为条件:
In [131]:
s1[(s1.index.isin(s2.index)) & (s1.isin(s2))]
Out[131]:
000007720
group1 -3732.05
group2 -38147.87
FSHLAJ -36711.09
Name: 2000.00, dtype: float64