条件避免无穷大的除法过程 python pandas
division process with condition avoiding infinites python pandas
我对我处理这个问题的方式有疑问
这段代码的作用基本上是:
x3 = (x2-x1)/x1
其中 x1 是第 d 天的值
其中 x2 是天 d+1 的值
x3 是我根据以前的值
计算的值
什么时候我的师是这样的:
例如,(0.5-0)/0 与 2017-09-010 和 POS_16_20_and 2017-09-011 发生的情况一样,它将是无限的。我想在除法中使用一个条件,如果我除的值为零,则设置 x3=x2 因为我不想要无限值
我想用我最后的值替换它。
代码:
waps_df2 = waps_df1-waps_df1.shift(1)
waps_df2 = waps_df2.fillna(0)
waps_x = waps_df2.div(waps_df1.shift(1))
waps_ad = waps_x.add(1)
waps_x3 = waps_ad.shift(+1)
我的约会框架:
produktname POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-10 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-11 0.0 0.0 0.0 0.0 0.05 0.0
2017-09-12 0.0 0.0 0.0 0.0 0.06 0.0
2017-09-13 0.0 0.0 0.0 0.0 0.00 0.0
我试过戴口罩
waps_pos = waps_pos.mask((waps_df1!=0), waps_pos.div(waps_df1.shift(1))
waps_x = np.where(waps_df1.shift(1)>0, waps_pos.div(waps_df1.shift(1), waps_df1)
或
waps_x = np.where(waps_df1.shift(1)>0, waps_pos.div(waps_df1.shift(1), waps_df1)
waps_df2 = waps_df1.sub(waps_df1.shift(1)).fillna(0)
print (waps_df2)
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-10 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-11 0.0 0.0 0.0 0.0 0.05 0.0
2017-09-12 0.0 0.0 0.0 0.0 0.01 0.0
2017-09-13 0.0 0.0 0.0 0.0 -0.06 0.0
waps_x = waps_df2.div(waps_df1.shift(1))
print (waps_x)
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 NaN NaN NaN NaN NaN NaN
2017-09-10 NaN NaN NaN NaN NaN NaN
2017-09-11 NaN NaN NaN NaN inf NaN
2017-09-12 NaN NaN NaN NaN 0.200000 NaN
2017-09-13 NaN NaN NaN NaN -1.000000 NaN
您可以通过 numpy.isinf
and replace them to waps_df1
by mask
:
检查 inf 值
print (np.isinf(waps_x))
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 False False False False False False
2017-09-10 False False False False False False
2017-09-11 False False False False True False
2017-09-12 False False False False False False
2017-09-13 False False False False False False
waps_x = waps_x.mask(np.isinf(waps_x), waps_df1)
print (waps_x)
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 NaN NaN NaN NaN NaN NaN
2017-09-10 NaN NaN NaN NaN NaN NaN
2017-09-11 NaN NaN NaN NaN 0.05 NaN
2017-09-12 NaN NaN NaN NaN 0.20 NaN
2017-09-13 NaN NaN NaN NaN -1.00 NaN
我对我处理这个问题的方式有疑问 这段代码的作用基本上是:
x3 = (x2-x1)/x1
其中 x1 是第 d 天的值 其中 x2 是天 d+1 的值 x3 是我根据以前的值
计算的值什么时候我的师是这样的: 例如,(0.5-0)/0 与 2017-09-010 和 POS_16_20_and 2017-09-011 发生的情况一样,它将是无限的。我想在除法中使用一个条件,如果我除的值为零,则设置 x3=x2 因为我不想要无限值
我想用我最后的值替换它。
代码:
waps_df2 = waps_df1-waps_df1.shift(1)
waps_df2 = waps_df2.fillna(0)
waps_x = waps_df2.div(waps_df1.shift(1))
waps_ad = waps_x.add(1)
waps_x3 = waps_ad.shift(+1)
我的约会框架:
produktname POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-10 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-11 0.0 0.0 0.0 0.0 0.05 0.0
2017-09-12 0.0 0.0 0.0 0.0 0.06 0.0
2017-09-13 0.0 0.0 0.0 0.0 0.00 0.0
我试过戴口罩
waps_pos = waps_pos.mask((waps_df1!=0), waps_pos.div(waps_df1.shift(1))
waps_x = np.where(waps_df1.shift(1)>0, waps_pos.div(waps_df1.shift(1), waps_df1)
或
waps_x = np.where(waps_df1.shift(1)>0, waps_pos.div(waps_df1.shift(1), waps_df1)
waps_df2 = waps_df1.sub(waps_df1.shift(1)).fillna(0)
print (waps_df2)
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-10 0.0 0.0 0.0 0.0 0.00 0.0
2017-09-11 0.0 0.0 0.0 0.0 0.05 0.0
2017-09-12 0.0 0.0 0.0 0.0 0.01 0.0
2017-09-13 0.0 0.0 0.0 0.0 -0.06 0.0
waps_x = waps_df2.div(waps_df1.shift(1))
print (waps_x)
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 NaN NaN NaN NaN NaN NaN
2017-09-10 NaN NaN NaN NaN NaN NaN
2017-09-11 NaN NaN NaN NaN inf NaN
2017-09-12 NaN NaN NaN NaN 0.200000 NaN
2017-09-13 NaN NaN NaN NaN -1.000000 NaN
您可以通过 numpy.isinf
and replace them to waps_df1
by mask
:
print (np.isinf(waps_x))
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 False False False False False False
2017-09-10 False False False False False False
2017-09-11 False False False False True False
2017-09-12 False False False False False False
2017-09-13 False False False False False False
waps_x = waps_x.mask(np.isinf(waps_x), waps_df1)
print (waps_x)
POS_00_04 POS_04_08 POS_08_12 POS_12_16 POS_16_20 POS_20_24
datum_von
2017-09-09 NaN NaN NaN NaN NaN NaN
2017-09-10 NaN NaN NaN NaN NaN NaN
2017-09-11 NaN NaN NaN NaN 0.05 NaN
2017-09-12 NaN NaN NaN NaN 0.20 NaN
2017-09-13 NaN NaN NaN NaN -1.00 NaN