Highlight dataframe cells based on multiple conditions in Python

别等时光非礼了梦想. 提交于 2020-12-13 05:40:05

问题


Given a small dataset as follows:

   id    room   area           situation
0   1   A-102  world  under construction
1   2     NaN     24  under construction
2   3    B309    NaN                 NaN
3   4   C·102     25    under decoration
4   5  E_1089  hello    under decoration
5   6      27    NaN          under plan
6   7      27    NaN                 NaN

Thanks to the code from @jezrael at this link, I'm able to get the result I needed:

a = np.where(df.room.str.match('^[a-zA-Z\d\-]*$', na = False), None,
                               'incorrect room name')
b = np.where(df.area.str.contains('^\d+$', na = True), None,
                                 'area is not a numbers')  
c = np.where(df.situation.str.contains('under decoration', na = False),
                                      'decoration is in the content', None) 

f = (lambda x: '; '.join(y for y in x if pd.notna(y)) 
                if any(pd.notna(np.array(x))) else np.nan )
df['check'] = [f(x) for x in zip(a,b,c)]
print(df)

   id    room   area           situation  \
0   1   A-102  world  under construction   
1   2     NaN     24  under construction   
2   3    B309    NaN                 NaN   
3   4   C·102     25    under decoration   
4   5  E_1089  hello    under decoration   
5   6      27    NaN          under plan   
6   7      27    NaN                 NaN   

                                               check  
0                              area is not a numbers  
1                                incorrect room name  
2                                                NaN  
3   incorrect room name;decoration is in the content  
4  incorrect room name;area is not a numbers;deco...  
5                                                NaN  
6                                                NaN  

But now I would like to go further and hightlight the problematic cells from room, area, situation columns, then save the dataframe as excel file.

How could I do that in Pandas (better) or other Python packages?

Thanks at advance.


回答1:


Idea is to create customized function for return DataFrame of styles and reuse m1, m2, m3 boolean masks:

m1 = df.room.str.match('^[a-zA-Z\d\-]*$', na = False)
m2 = df.area.str.contains('^\d+$', na = True)
m3 = df.situation.str.contains('under decoration', na = False)
a = np.where(m1, None, 'incorrect room name')
b = np.where(m2, None, 'area is not a numbers')  
c = np.where(m3, 'decoration is in the content', None) 


f = (lambda x: '; '.join(y for y in x if pd.notna(y)) 
                if any(pd.notna(np.array(x))) else np.nan )
df['check'] = [f(x) for x in zip(a, b, c)]
print(df)


def highlight(x):
    c1 = 'background-color: yellow'

    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    df1['room'] = np.where(m1, '', c1)
    df1['area'] = np.where(m2, '', c1)
    df1['situation'] = np.where(m3, c1, '')
    # print(df1)
    return df1

df.style.apply(highlight, axis = None).to_excel('test.xlsx', index = False)


来源:https://stackoverflow.com/questions/64766324/highlight-dataframe-cells-based-on-multiple-conditions-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!