highlight (color) a panda data frame row by index

前端 未结 2 1216
情书的邮戳
情书的邮戳 2021-02-06 14:01

i have two data frame df1:

d1 = {\"col1\" : [\'A\', \'B\', \'C\'],
      \"Col2\": [\"home\", \"car\",\"banana\" ]}

d2 = {\"col1\" : [\'D\', \'F\',\'C\'],
              


        
2条回答
  •  自闭症患者
    2021-02-06 14:37

    In case you want to highlight two rows (say index 2 and 4) it is a almost a duplicate of this answer

    new_df.style.apply(lambda x: ['background: lightgreen' if x.name in [2,4] 
                                  else '' for i in x], 
                       axis=1)
    

    If instead you are looking to highlight every row that contain a given name in a list (i.e. lst = ['car', 'boat']) you can use

    new_df.style.apply(lambda x: ['background: lightgreen' if (set(lst).intersection(x.values)) 
                                  else '' for i in x], 
                       axis=1)
    

提交回复
热议问题