python pandas选择两列(不)相等的行 [英] python pandas select rows where two columns are (not) equal
本文介绍了python pandas选择两列(不)相等的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hsp.loc[hsp['Len_old'] == hsp['Len_new']]
我尝试使用此代码,它可以正常工作.
I try this code, it's working.
但是我尝试了这三个
hsp.loc[hsp['Type_old'] == hsp['Type_new']]
hsp.loc[hsp['Type_old'] != hsp['Type_new']]
hsp.loc[hsp['Len_old'] != hsp['Len_new']]
它们不起作用.
我的数据表hsp就像
id Type_old Type_new Len_old Len_new
1 Num Num 15 15
2 Num Char 12 12
3 Char Num 10 8
4 Num Num 4 5
5 Char Char 9 10
有没有一种更好的方法来选择两列都不合格的行.
Is there a better approach to select rows where two columns are not queal.
推荐答案
比较pd.Series
符合预期
df[['Len_old', 'Len_new']].assign(NE=df.Len_old != df.Len_new)
Len_old Len_new NE
0 15 15 False
1 12 12 False
2 10 8 True
3 4 5 True
4 9 10 True
但是,如果该列的值之一是字符串!
df[['Len_old', 'Len_new']].assign(NE=df.Len_old.astype(str) != df.Len_new)
Len_old Len_new NE
0 15 15 True
1 12 12 True
2 10 8 True
3 4 5 True
4 9 10 True
确保两种类型相同.
Make sure both are the same types.
这篇关于python pandas选择两列(不)相等的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文