简体   繁体   English

使用python在数据帧中进行条件格式化

[英]conditional formatting in dataframe using python

I have a pandas data frame and I need to classify it based on the specified condition. 我有一个pandas数据框,我需要根据指定的条件对其进行分类。 The threshold is fixed and it has to be classified based on 8 different combinations of the threshold. 阈值是固定的,必须根据阈值的8种不同组合进行分类。

Threshold (A => 7, B = 3 or 4, C = between 22 - 27) 

I tried using pandas with conditional operations to classify the data but it produces misleading results. 我尝试使用带有条件操作的pandas对数据进行分类,但会产生误导性结果。

Conditions are: 条件是:

1. class1=f[(f['A']>7.0)&((f['B']==3.0)|(f['B']==4.0))& ((f['C']>=22.0)&(f['C']<=27.0))]
2. class2=f[(f['A']>7.0)&((f['B']==3.0)|(f['B']==4.0))& ((f['C']<=22.0)&(f['C']>=27.0))]
3. class3=f[(f['A']<7.0)&((f['B']==3.0)|(f['B']==4.0))& ((f['C']>=22.0)&(f['C']<=27.0))]
4. class4=f[(f['A']>7.0)&((f['B']!=3.0)&(f['B']!=4.0))& ((f['C']>=22.0)&(f['C']<=27.0))]
5. class5=f[(f['A']>7.0)&((f['B']!=3.0)&(f['B']!=4.0))& ((f['C']<=22.0)&(f['C']>=27.0))]
6. class6=f[(f['A']<7.0)&((f['B']==3.0)|(f['B']==4.0))& ((f['C']<=22.0)&(f['C']>=27.0))]
7. class7=f[(f['A']<7.0)&((f['B']!=3.0)&(f['B']!=4.0))& ((f['C']>=22.0)&(f['C']<=27.0))]
8. class8=f[(f['A']<7.0)|((f['B']!=3.0)&(f['B']!=4.0))| ((f['C']<=22.0)&(f['C']>=27.0))]

I need all rows in the data frame to be classified based on the conditions. 我需要根据条件对数据框中的所有行进行分类。

Your situation : your dataframe is called f and contains 3 columns with numeric values. 您的情况 :您的数据框名为f ,包含3列数字值。 The columns are called 'A' , 'B' and 'C' . 这些列称为'A''B''C'

I recommend doing it by creating boolean columns and combining them to match your classes. 我建议通过创建布尔列并将它们组合以匹配您的类来实现。 There are probably many more ways to do this, also much more elegant ones. 可能还有很多方法可以做到这一点,也有更优雅的方法。 I think this solution is as simple as they get. 我认为这个解决方案就像他们得到的一样简单。 Essentially, you have three conditions that can be met: 基本上,您有三个条件可以满足:

check_a = f['A'] >= 7
check_b = (f['B'] == 3) | (f['B'] == 4)
check_c = (22 <= f['C'] <= 27)

Combining these 3 checks will construct your 8 cases ( ~ negates the booleans, so basically flipping their values): 结合这3个检查将构建你的8个案例( ~否定布尔值,所以基本上翻转它们的值):

f['class_1'] =  check_a &  check_b &  check_c
f['class_2'] =  check_a &  check_b & ~check_c
f['class_3'] = ~check_a &  check_b &  check_c
f['class_4'] =  check_a & ~check_b &  check_c
f['class_5'] =  check_a & ~check_b & ~check_c
f['class_6'] = ~check_a &  check_b & ~check_c
f['class_7'] = ~check_a & ~check_b &  check_c
f['class_8'] = ~check_a & ~check_b & ~check_c

One of the reason your code doesn't work, is that you are checking whether the values in column 'C' are both smaller than 22 AND larger than 27. This can never be true. 您的代码不起作用的原因之一是您要检查'C'列中的值是否小于22且大于27.这可能永远不会成立。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM