I am looking to compare 2 Series in Pandas. I have the following code
a = pd.Series([1, 2, 3, 4], index = [1, 2, 3, 4])
b = pd.Series([1, 1, 1], index = [1, 5, 4])
a[~a.eq(b)]
In the case when the Series is empty, ie, no values have been added to it as yet.
b = pd.Series()
a[~a.eq(b)]
Both cases give me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 735, in wrapper
raise ValueError('Series lengths must match to compare')
ValueError: Series lengths must match to compare
The value I am looking to arrive at is:
2 2
3 3
4 4
in the 1st case and the following in the latter case:
1 1
2 2
3 3
4 4
I think you need Series.reindex
for same indices:
print (b.reindex(a.index))
1 1.0
2 NaN
3 NaN
4 1.0
dtype: float64
print (a[~a.eq(b.reindex(a.index))])
2 2
3 3
4 4
dtype: int64
print (a[~a.eq(b.reindex(pd.Series([]).index))])
1 1
2 2
3 3
4 4
dtype: int64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.