我有这样的温度数据:
Date State Temperature (F)
2018-10-6 AL 15
2018-10-7 AL 45
2018-10-8 AL 67
2018-10-9 AL 25
2018-10-10 AL 55
2018-10-11 AL 77
.
.我想给出一个条件语句,它计算一个简单的减法,创建一个新的列,如下所示:
if df[Temperature, i] < 65:
df[Calculation, i] = 65 - df[Temperature, i]
else:
df[Calculation, i] = 0因此,产出如下:
Date State Temperature (F) Calculation
2018-10-6 AL 15 0
2018-10-7 AL 45 0
2018-10-8 AL 67 2
2018-10-9 AL 25 0
2018-10-10 AL 55 0
2018-10-11 AL 77 12
.
.我不知道如何使用这种类型的循环简单地遍历列。
有什么简单的方法吗?
发布于 2019-01-07 20:50:38
IIUC clip_lower
df['Temperature(F)'].sub(65).clip_lower(0)
Out[377]:
0 0
1 0
2 2
3 0
4 0
5 12
Name: Temperature(F), dtype: int64更新14.05.2021:
与clip_lower(0)不同的是,关于熊猫的文档为0.25.3,lower.html声明:
从0.24.0版本开始被废弃:使用剪辑(lower=threshold)代替。
所以答案是:
df['Temperature (F)'].sub(65).clip(lower = 0)
Out[6]:
0 0
1 0
2 2
3 0
4 0
5 12
Name: Temperature (F), dtype: int64https://stackoverflow.com/questions/54081618
复制相似问题