数据框上的多个条件 [英] multiple conditions on dataframes

查看:58
本文介绍了数据框上的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个新列'is_good',如果'value'列中的数据集介于 1到6 之间并且'value2'列位于范围从5到10 ,如果它们都不满足这两个条件,则将它们标记为0

I'm trying to write a new column 'is_good' which is marked 1 if the data sets in 'value' column is between range 1 to 6 and when 'value2' column is in range 5 to 10 if they do not satisfy both condition they are marked 0

我知道你是否这样做,

df['is_good'] = [1 if (x >= 1 and x <= 6) else 0 for x in df['value']]

它将根据值的范围填写1或0,但是在标记1或0时我还将如何考虑value2的范围.

it will fill out 1 or 0 depending on the ranges of value but how would I also consider ranges of value2 when marking 1 or 0.

反正我可以不用numpy来实现吗?

Is there anyway I can achieve this without numpy?

先谢谢您!

推荐答案

我认为需要在

I think need double between and chain conditions by & (bitwise and):

df = pd.DataFrame({'value':range(13),'value2':range(13)})
df['is_good'] =  (df['value'].between(1,6) & df['value2'].between(5,10)).astype(int)

或使用4个条件:

df['is_good'] =  ((df['value'] >= 1) & (df['value'] <= 6) & 
                   (df['value2'] >= 5) & (df['value'] <= 10)).astype(int) 


print (df)
    value  value2  is_good
0       0       0        0
1       1       1        0
2       2       2        0
3       3       3        0
4       4       4        0
5       5       5        1
6       6       6        1
7       7       7        0
8       8       8        0
9       9       9        0
10     10      10        0
11     11      11        0
12     12      12        0

这篇关于数据框上的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆