简体   繁体   English

Pandas DataFrame 用最新的前一个正值替换负值

[英]Pandas DataFrame replace negative values with latest preceding positive value

Consider a DataFrame such as考虑一个DataFrame例如

df = pd.DataFrame({'a': [1,-2,0,3,-1,2], 
                   'b': [-1,-2,-5,-7,-1,-1], 
                   'c': [-1,-2,-5,4,5,3]})

For each column, how to replace any negative value with the last positive value or zero?对于每一列,如何用最后一个正值或零替换任何负值? Last here refers from top to bottom for each column.最后这里指的是每列从上到下。 The closest solution noticed is for instance df[df < 0] = 0 .注意到的最接近的解决方案是例如df[df < 0] = 0

The expected result would be a DataFrame such as预期的结果将是DataFrame例如

df_res = pd.DataFrame({'a': [1,1,0,3,3,2], 
                       'b': [0,0,0,0,0,0], 
                       'c': [0,0,0,4,5,3]})

You can use DataFrame.mask to convert all values < 0 to NaN then use ffill and fillna :您可以使用DataFrame.mask将所有< 0的值转换为NaN然后使用ffillfillna

df = df.mask(df.lt(0)).ffill().fillna(0).convert_dtypes()
   a  b  c
0  1  0  0
1  1  0  0
2  0  0  0
3  3  0  4
4  3  0  5
5  2  0  3

Use pandas where使用 pandas 其中

df.where(df.gt(0)).ffill().fillna(0).astype(int)



   a  b  c
0  1  0  0
1  1  0  0
2  1  0  0
3  3  0  4
4  3  0  5
5  2  0  3

Expected result may obtained with this manipulations:通过这种操作可以获得预期的结果:

mask = df >= 0 #creating boolean mask for non-negative values
df_res = (df.where(mask, np.nan) #replace negative values to nan
          .ffill() #apply forward fill for nan values 
          .fillna(0)) # fill rest nan's with zeros

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM