highlight (color) a panda data frame row by index

前端 未结 2 1187
情书的邮戳
情书的邮戳 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:38

    Lots of questions link here regarding styling a DataFrame row, so I kept coming here despite the accepted answer not working for me, and no useful error message from pandas. I just came to point out that while the accepted answer may work fine for html, or Jupyter or whatever they're using. It does not work when applied to Excel. For Excel you'll need the following:

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

    Note the use of "backgound-color". Excel will not render "background" as you might expect

    In addition, when working with Excel, you'll need to use a color name From Excel's list of recognized colors, Fiddling around with the above answer using "lightgreen" may give you black-on-black... probably not what anyone is looking for.

提交回复
热议问题