python - 条件避免无限python pandas的划分过程

标签 python pandas numpy infinite

我对我处理这个问题的方式有疑问 这段代码的作用基本上是:

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 检查 inf 值并通过 mask 将它们替换为 waps_df1 :

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

关于python - 条件避免无限python pandas的划分过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182614/

相关文章:

Python Pandas 数据帧 : How to process a column consisting of dicts into multiple columns determined by the keys of the dicts?

python - 如何将日期时间格式化为 "2012-12-11 12:00:00"

Python,使用列表中的列表,还是列表中的对象?

python - Pytorch 特定操作,用于查找张量列表的维度均值

python - 获取Gql中包含单词(不区分大小写)的记录?

python - Pytest:失败时如何对 Pandas DataFrame 进行快照测试

python - 将 pandas DataFrame 制作成 dict 和 dropna

python - 如何根据Python中的时间戳索引将行转置为单列?

python - 基于另一个相似矩阵对矩阵进行排序的Numpyic方法

python - 计算每个场点在轮廓内的频率