简体   繁体   English

如何通过每列的不同条件突出显示 dataframe 中的某些单元格? 是否也可以突出显示系列中的某些数字?

[英]How to highlight some cells in a dataframe through different condition for each column? Is also possible highlighting some number in a Series?

I'm working with Python and I have a serious (I really can't understand how I can solve it) problem for highlighting some cells in my dataframe.我正在使用 Python 并且我有一个严重的(我真的无法理解如何解决它)问题来突出我的 dataframe 中的一些单元格。 I show you my doubt: I have 2 matrix and the shape is the same in both of them.我向您展示我的疑问:我有 2 个矩阵,并且它们的形状都相同。 One of this matrix is made just by 0/1 and the other matrix has decimal numbers.该矩阵之一仅由 0/1 组成,另一个矩阵具有十进制数。 I would highlight the element in the matrix made by decimal numbers but with a condition on the matrix made by 0/1.我将突出显示矩阵中由十进制数字组成的元素,但矩阵的条件由 0/1 组成。 So for example: if in the matrix made by 0/1 I have a cell with 1 THEN I would highlight the same cell in the other matrix (ex: in my matrix you can see that in the first cell I have 1 so I would highlight the first cell of the other matrix (0.000034)).例如:如果在由 0/1 组成的矩阵中我有一个单元格为 1 那么我将突出显示另一个矩阵中的相同单元格(例如:在我的矩阵中你可以看到在第一个单元格中我有 1 所以我会突出显示另一个矩阵的第一个单元格(0.000034))。 I really don't know how I can write this.我真的不知道我该怎么写这个。

I also thought to divide my dataframe in singular column and working with one by one and then put all column together, maybe could be easier?我还想将我的 dataframe 分成单个列并一一处理,然后将所有列放在一起,也许会更容易? but...I didn't find how I can highlight some element in a Series because notebook sayes to me that Series hasn't attribute "apply" (when I write "Series.style.applyect). Is it possible?但是......我没有找到如何突出显示系列中的某些元素,因为笔记本对我说系列没有属性“应用”(当我写“系列.style.applyect”时)。这可能吗?

Thank you for your help!谢谢您的帮助!

Result= pd.DataFrame({'First':[1,1,1,1,1, ...], 'Second': [1,1,1,0,1, ...], 'Third': [0,0,0,0,0, ...], 'Fourth':[1,1,1,1,0, ...]})结果= pd.DataFrame({'First':[1,1,1,1,1, ...], 'Second': [1,1,1,0,1, ...], '第三' : [0,0,0,0,0, ...], '第四':[1,1,1,1,0, ...]})

Values= pd.DataFrame({'One':[0.000034, 0.000043, 0.000023, 0.000055, 0.000034, ...], 'Two': [0.000011, 0.000087, 0.000045, 0.000065, 0.000024, ...], 'Three': [0.000054, 0.000026, 0.000043, 0.000022, 0.000024...], 'Four':[0.000011, 0.000025, 0.000053, 0.000036, 0.000026, ...]})值= pd.DataFrame({'一':[0.000034, 0.000043, 0.000023, 0.000055, 0.000034, ...], '二': [0.000011, 0.000087, 0.000045, 0.000065, 0.000024, ...'], 三' : [0.000054, 0.000026, 0.000043, 0.000022, 0.000024...], '四': [0.000011, 0.000025, 0.000053, 0.000036, 0.000026, ...]})

import pandas as pd
df = pd.DataFrame([[2,3,1], [3,2,2], [2,4,4]], columns=list("ABC"))
df_mask = pd.DataFrame([[1,0,1], [0,0,0], [0,1,1]], columns=list("ABC"))


def highlight_mask(df, df_mask, color='yellow'):
    '''
    highlight the cells with df_mask.
    '''
    df_mask.replace(1, 'background-color: {}'.format(color), inplace=True)
    df_mask.replace(0, '', inplace=True)
    return df_mask


df.style.apply(highlight_mask, df_mask=df_mask, color='red', axis = None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 数据框样式:根据格式列突出显示一些单元格 - Pandas dataframe styling: highlight some cells based on a format column Python Pandas 样式突出显示具有不同条件的每列的特定单元格 - Python Pandas style highlight specific cells for each column with different condition 如何向数据框中的每一列输入一些值 - How to input some values to each column in dataframe 如何在某些条件下重命名pandas数据帧中的列值 - how to rename a column value in pandas dataframe on some condition 如何根据 dataframe 中的某些条件修改列值? - How to modify column value based on some condition in a dataframe? 在某些情况下,如何将一列乘以一个数字? - How can I multiply one column by a number, under some condition? 如何将 Series 合并到 DataFrame 中 Series 索引匹配某些列值? - How to merge Series to DataFrame where Series index match some column value? 如何为 pandas 样式编写 function 读取 dataframe 名称和值条件和 Z628481533CE5804AA87B2 单元格 - How write a function for pandas style that reads dataframe name and values condition and colors some cells? dataframe 在某些条件下为每行拆分为多个 dataframe - dataframe split to multiple dataframe for each rows with some condition 将 dataframe 中具有某些条件的每一列的值相加以创建另一个 dataframe - Sum values of each columns with some condition in dataframe to create another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM