“Sel Warna DataFrame” Kode Jawaban

Sel Warna DataFrame

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

Sel Warna DataFrame

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

Jawaban yang mirip dengan “Sel Warna DataFrame”

Pertanyaan yang mirip dengan “Sel Warna DataFrame”

Lebih banyak jawaban terkait untuk “Sel Warna DataFrame” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya