Python 基于多种条件设计熊猫?

Python 基于多种条件设计熊猫?,python,pandas,Python,Pandas,我想根据2-3个条件为一些行着色: df 我想根据所有三列填充背景 i、 e 我不认为我能够理解样式如何工作的概念。有人能举例说明一下吗 提前谢谢 您可以使用loc创建样式的数据框,并按条件设置行: 非常感谢。你是最有价值球员。 status days_since_claim claim_action 0 Closed 349 days No action 1 Closed 353 days No action

我想根据2-3个条件为一些行着色:

df

我想根据所有三列填充背景

i、 e

我不认为我能够理解样式如何工作的概念。有人能举例说明一下吗


提前谢谢

您可以使用loc创建样式的数据框,并按条件设置行:


非常感谢。你是最有价值球员。
    status days_since_claim claim_action
0   Closed         349 days  No action       
1   Closed         353 days  No action           
2  Granted         373 days  Check account           
3  Granted         431 days  Account checked           
4   Closed         448 days  No action
`backgroud_color: 'green' if 'status' == 'Closed' and claim_action == 'No action'

`backgroud_color: 'red' if 'status' == 'Granted' and claim_action == 'Check account' and 'days_since_claim' > 300`

I tried:

styled = mdf.style.applymap(lambda v: 'background-color: %s' %
                                      'red' if v > 300 else "")
def color_s(df):
    for i, row in df.iterrows():
        if row['status'] == 'Closed':
                 .
                 .
def color(x):
    c1 = 'background-color: green'
    c2 = 'background-color: red'
    c = '' 
    #compare columns
    mask1 = (x['status'] == 'Closed') & 
            (x['claim_action'] == 'No action')
    mask2 = (x['status'] == 'Granted') & 
            (x['claim_action'] == 'Check account') & 
            (x['days_since_claim'].dt.days > 300)
    #DataFrame with same index and columns names as original filled empty strings
    df1 =  pd.DataFrame(c, index=x.index, columns=x.columns)
    #modify values of df1 column by boolean mask
    df1.loc[mask1, :] = c1
    df1.loc[mask2, :] = c2
    return df1

df.style.apply(color, axis=None)