.eq()在比较数据帧时的问题



我试图使用.eq()比较两个数据帧(df和df2),但它给了我假。我确定值:

打印(df(去年的))2021年
0
名称:去年,dtype: int64

print(df2['ano'])
0 2020
1 2019
2 2019
3 2018
4 2017

89 2020
90 2017
91 2018
92 2021
93 2021
Name: ano, Length: 94, dtype: int64

print(df['ano'].eq(df2['ano']))
0 False
1 False
2 False
3 False
4 False

89 False
90 False
91 False
92 False
93 False
Name: ano, Length: 94, dtype: bool

Series.eq(other)比较两个系列元素,如果您想比较df2['ano']df1['ano']中的第一个元素,您可以使用Series.item()Series.iloc[0]

print(df2['ano'].eq(df['ano'].item()))
# or
print(df2['ano'].eq(df['ano'].iloc[0]))

如果你想检查df['ano']中的元素是否在df2['ano']中,你可以使用Series.isin

print(df['ano'].isin(df2['ano']))

解决方案:

<>以前df = df.drop_duplicates x = 0 ()

y = 0
df = df.reset_index ()
df2 = df2.reset_index ()
, (x = len (df)):
, (y = len (df2)):
如果(df)。在[x, a] = = df2。At [y, 'a']) & (df。At [x, 'b'] == df2。At [y, 'b']) & (df。At [x, 'c'] == df2。在[y, ' c '])):
打印(发现)Y += 1X += 1Y = 0