Pandas- Dividing a column by another column conditional on if values are greater than 0?

后端 未结 2 1066
北恋
北恋 2021-01-05 08:51

I have a pandas dataframe that contains dates, items, and 2 values. All I\'m looking to do is output another column that is the product of column A / column B if column B is

2条回答
  •  离开以前
    2021-01-05 09:22

    You get that using np.where

    df['C'] = np.round(np.where(df['B'] > 0, df['A']/df['B'], 0), 1)
    

    Or if you want to use loc

    df.loc[df['B'] > 0, 'C'] = df['A']/df['B']
    

    and then fillna(0)

提交回复
热议问题