Highlight cell on condition

后端 未结 2 522
礼貌的吻别
礼貌的吻别 2021-01-27 00:32

I\'m trying to highlight all cells that are before the current date. However I am highlighting all cells instead of just the old dates.

import pandas as pd
from         


        
2条回答
  •  情话喂你
    2021-01-27 01:18

    applymap is executed on each cell. You don't need to loop over each row if using this. However, it seems you are trying to highlight the entire row, so you likely want apply by row. Using this method, you have to return an array with the same size as each row.

    def expired(val):
        return ['background-color: green' if val['Expiration Date'] else ''] * len(val)
    
    new = df.style.apply(expired, axis=1)
    

提交回复
热议问题