【问题标题】:Match columns and Highlight results in pandas dataframe python在熊猫数据框python中匹配列并突出显示结果
【发布时间】: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'”,'发生在索引样本')”

这是一个输入示例:

它应该是这样的:

有人能指出我正确的方向吗?谢谢

【问题讨论】:

  • 提供输入数据框的示例,以及您的预期输出。
  • 我编辑了我的问题以获得所需的输入

标签: python pandas window


【解决方案1】:

如果要突出显示 Names 列中的重复值,请使用 Styler.applyDataFrame.filter 创建的掩码,用于带有 Name 的列,由 DataFrame.stack 重新整形,通过 Series.duplicated 获取重复值并通过 @987654325 重新整形@:

df=pd.DataFrame({'Name':['Dog','Dog.1','Dog.3'],'ID':[1,2,3],'type':[1.1,1.2,1.3],
                 'Name.1':['Dog','cat','Dog.3'],'ID.1':[1,2,3],'type.1':[1.1,1.2,1.3],
                 'Name.2':['cat','cat.12','Dog.1'],'ID.2':[1,2,3],'type.2':[1.1,1.2,1.3],
                 'Name.3':['cat.7','cat.13','Dog.3'],'ID.3':[1,2,3],'type.3':[1.1,1.2,1.3]})

print (df)
    Name  ID  type Name.1  ID.1  type.1  Name.2  ID.2  type.2  Name.3  ID.3  \
0    Dog   1   1.1    Dog     1     1.1     cat     1     1.1   cat.7     1   
1  Dog.1   2   1.2    cat     2     1.2  cat.12     2     1.2  cat.13     2   
2  Dog.3   3   1.3  Dog.3     3     1.3   Dog.1     3     1.3   Dog.3     3   

   type.3  
0     1.1  
1     1.2  
2     1.3  

def color(x):
    c1 = 'background-color: yellow'
    c = '' 
    #compare columns
    names = x.filter(like='Name')
    mask = names.stack().duplicated(keep=False).unstack()
    #DataFrame with same index and columns names as original filled empty strings
    df1 =  pd.DataFrame(c, index=x.index, columns=x.columns)
    #modify values of df1 column by boolean mask
    df1 = names.mask(mask, c1).reindex(df1.columns, axis=1, fill_value='')
    return df1


df.style.apply(color, axis=None).to_excel('df.xlsx', engine='openpyxl', index=False)

【讨论】:

  • 您也可以将输出写入excell文件吗?因为当我尝试这样做时,我收到此错误:“结果必须具有与输入相同的索引和列”
  • @Sofia - 我尝试了解绿色突出显示值,但失败了。你能解释一下吗?还有Please don't post images of code/data (or links to them)
  • 所以 dog 在相同的三列中重复,然后 cat 也重复。就是这样,它被重复了......基本上绿色与其他列中重复的相同单词匹配。
  • @jezrael- 我刚刚在 pandas 中添加了数据框,感谢您的帮助 :)
  • 我编辑了 pandas 数据框,向您展示我的真实情况。因为我收到此错误:一元 ~:'float' 的操作数类型错误。你能看看你是否得到这个错误?很抱歉给您带来麻烦。
猜你喜欢
  • 2019-01-06
  • 1970-01-01
  • 2021-08-08
  • 2020-05-25
  • 2022-08-19
  • 1970-01-01
  • 2018-10-10
  • 2018-10-01
  • 2021-02-03
相关资源
最近更新 更多