我想比较 2 个熊猫系列,并在两个系列中找到相同的条目,但区分大小写。
类似于下面的例子:
df['Comment']=np.where((df['Identifier_1']==df['Identifier_2']),'The same',df['Comment'])
但是条件应该以某种方式更改,以便如果 Identifier_1='aBc' 和 Identifier_2='Abc' 然后返回 True。
谢谢和BR
使用lower():
df['Comment']=np.where(df['Identifier_1'].astype(str).lower() == df['Identifier_2'].astype(str).lower(),'The same',df['Comment'])
这会将两个字符串默认为小写,以便可以根据需要对它们进行比较。这样AbC和aBc将分别变成abc和abc,返回True
df = pd.DataFrame({'Identifier_1':['aBc','aBC'],
'Identifier_2':['Abc','ABZ'],
'Comment':['com1','com2']})
df['Comment']=np.where(df['Identifier_1'].str.lower() == df['Identifier_2'].str.lower(),
'The same',
df['Comment'])
print (df)
Identifier_1 Identifier_2 Comment
0 aBc Abc The same
1 aBC ABZ com2
只需使用str.lower()使它们都小写。
df['Comment']=np.where(df['Identifier_1'].str.lower().equals(df['Identifier_2'].str.lower()),
'The same',
df['Comment'])