[英]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.