[Python] how to export data frame with color to csv [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I want to add color to cells of Status Pass and Fail. I tried following code it will print a colored data frame but exported csv was not colored.

    And when I applied two rules (green and red), only green shows.

    df = pd.DataFrame([{'Status':'Pass', 'Value': '0'}, {'Status':'Pass', 'Value': '1'}, {'Status':'FAIL', 'Value': '2'}])
    # add background color green to all lines with status 'PASS'
    df.to_csv('test.csv')
    df.style.map(lambda x: 'background-color: green' if x == 'Pass' else '', subset=['Status'])

    Continue reading...

Compartilhe esta Página