[英]Replacing values in dataframe with 0s and 1s based on conditions
I would like to filter and replace. 我想过滤并更换。 For the columns with are lower or higher than zero and not NaN's, I would like to set for one and the others, set to zero.
对于具有小于或大于零而不是NaN的列,我想将一个列和另一个列设置为零。
mask = ((ts[x] > 0)
| (ts[x] < 0))
ts[mask]=1
ts[ts[x]==1]
I did this and is working but I have to deal with the values that do not attend this condition replacing with zero. 我这样做了并且正在工作,但是我必须处理不符合此条件的值,将其替换为零。 Any recommendations?
有什么建议吗? I am quite confusing, and also would be better to use where function in this case?
我很困惑,在这种情况下在哪里使用功能也更好?
Thanks all! 谢谢大家!
Sample Data 样本数据
asset.relativeSetpoint.350
0 -60.0
1 0.0
2 NaN
3 100.0
4 0.0
5 NaN
6 -120.0
7 -245.0
8 0.0
9 123.0
10 0.0
11 -876.0
Expected result 预期结果
asset.relativeSetpoint.350
0 1
1 0
2 0
3 1
4 0
5 0
6 1
7 1
8 0
9 1
10 0
11 1
如何使用apply
?
df[COLUMN_NAME] = df[COLUMN_NAME].apply(lambda x: 1 if x != 0 else 0)
You can do this by applying a logical AND on the two conditions and converting the resultant mask to integer. 您可以通过在两个条件上应用逻辑“与”并将结果掩码转换为整数来实现。
df
asset.relativeSetpoint.350
0 -60.0
1 0.0
2 NaN
3 100.0
4 0.0
5 NaN
6 -120.0
7 -245.0
8 0.0
9 123.0
10 0.0
11 -876.0
(df['asset.relativeSetpoint.350'].ne(0)
& df['asset.relativeSetpoint.350'].notnull()).astype(int)
0 1
1 0
2 0
3 1
4 0
5 0
6 1
7 1
8 0
9 1
10 0
11 1
Name: asset.relativeSetpoint.350, dtype: int64
The first condition df['asset.relativeSetpoint.350'].ne(0)
gets a boolean mask of all elements that are not equal to 0 (this would include <0, >0, and NaN). 第一个条件
df['asset.relativeSetpoint.350'].ne(0)
获取所有不等于0的元素的布尔掩码(这将包括<0,> 0和NaN)。
The second condition df['asset.relativeSetpoint.350'].notnull()
will get a boolean mask of elements that are not NaNs. 第二个条件
df['asset.relativeSetpoint.350'].notnull()
将获得非NaN元素的布尔掩码。
The two masks are ANDed, and converted to integer. 两个掩码进行“与”运算,然后转换为整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.