“DataFrame -Farbzellen” Code-Antworten

DataFrame -Farbzellen

import pandas as pd
import numpy as np

def highlight_max(s):
    is_max = s == s.max()
    return ['background-color: red' if v else '' for v in is_max]

df.style.apply(highlight_max)
Peng Wenxuan

DataFrame -Farbzellen

import pandas as pd

df = pd.DataFrame({
    "name":         ["alan","beth","charlie","david", "edward"],
    "age" :         [34,    12,     43,      32,      77],
    "num_children": [1,     0,      2,       1,       6],
    "num_pets":     [1,     0,      1,       2,       0],
    "bank_balance": [100.0, 10.0,   -10.0,   30.0,    30.0]})

def more_children_or_more_pets_background(row):    

    highlight = 'background-color: lightcoral;'
    default = ''

    # must return one string per cell in this row
    if row['num_children'] > row['num_pets']:
        return [highlight, default]
    elif row['num_pets'] > row['num_children']:
        return [default, highlight]
    else:
        return [default, default]

df.style.apply(more_children_or_more_pets_background, subset=['num_children', 'num_pets'], axis=1)
Peng Wenxuan

Ähnliche Antworten wie “DataFrame -Farbzellen”

Fragen ähnlich wie “DataFrame -Farbzellen”

Weitere verwandte Antworten zu “DataFrame -Farbzellen” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen