【发布时间】:2020-02-05 11:38:27
【问题描述】:
我想在熊猫数据框中找到两列之间的匹配项并突出显示两列的结果,所以我做了以下代码:
df=pd.DataFrame({'Name':['Zea mays','Zea mays subsp. mexicana','Zea mays subsp. parviglumis'],'ID':[1,2,3],'type':[1.1,1.2,1.3],
'Name.1':['Zea mays subsp. huehuetenangensis','Zea mays subsp. mays','Zea mays'],'ID.1':[1,2,3],'type.1':[1.1,1.2,1.3],
'Name.2':['Zea nicaraguensis','Zea luxurians','Zea perennis'],'ID.2':[1,2,3],'type.2':[1.1,1.2,1.3],
'Name.3':['Capsicum annuum','Capsicum frutescens','Capsicum chinense'],'ID.3':[1,2,3],'type.3':[1.1,1.2,1.3]})
def in_statements(s):
color = 'yellow'
if np.where(str(s.iloc[4]) == str(s.iloc[8])):
color = 'yellow'
else:
color = 'black'
return 'background-color: %s' % color
df.style.applymap(in_statements)
但是,它给了我这个错误:“(“'str'对象没有属性'iloc'”,'发生在索引样本')”
这是一个输入示例:
有人能指出我正确的方向吗?谢谢
【问题讨论】:
-
提供输入数据框的示例,以及您的预期输出。
-
我编辑了我的问题以获得所需的输入