pandas 比较数据框中的两列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40479968/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:23:38  来源:igfitidea点击:

compare two columns value in dataframe

pythonpandascomparemultiple-columns

提问by Kun OuYang

I have a csv data frame like below, I'd like to compare two column value and generate third column, if value is same will return True, not same return False, how to compare with pandas python?

我有一个如下所示的csv数据框,我想比较两列值并生成第三列,如果值相同将返回True,返回不相同False,如何与pandas python进行比较?

one two
1   a
2   b
3   a
4   b
5   5
6   6
7   7
8   8
9   9
10  10

回答by jezrael

You need if values are mixed (stringand int):

如果值混合(stringint),您需要:

df['three'] = df.one == df.two

But need to_numericif values are not mixed - dtypeof first column is intand second is objectwhat is obviously stringand in column oneare not NaNvalues, because to_numericwith parameter errors='coerce'return NaNfor non numeric values:

但是需要to_numeric如果值不混合 -dtype第一列是int,第二列object显然是什么string,列one中不是NaN值,因为to_numeric参数errors='coerce'返回NaN非数值:

print (pd.to_numeric(df.two, errors='coerce'))
0     NaN
1     NaN
2     NaN
3     NaN
4     5.0
5     6.0
6     7.0
7     8.0
8     9.0
9    10.0
Name: two, dtype: float64

df['three'] = df.one == pd.to_numeric(df.two, errors='coerce')
print (df)
   one two  three
0    1   a  False
1    2   b  False
2    3   a  False
3    4   b  False
4    5   5   True
5    6   6   True
6    7   7   True
7    8   8   True
8    9   9   True
9   10  10   True

Faster solution with Series.eq:

更快的解决方案Series.eq

df['three'] = df.one.eq(pd.to_numeric(df.two, errors='coerce'))
print (df)
   one two  three
0    1   a  False
1    2   b  False
2    3   a  False
3    4   b  False
4    5   5   True
5    6   6   True
6    7   7   True
7    8   8   True
8    9   9   True
9   10  10   True