简体   繁体   中英

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

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 greater than 0, and 0 if column B is equal to 0.

   date     item   A   B        C       
 1/1/2017   a      0   3             0  
 1/1/2017   b      2   0             0  
 1/1/2017   c      5   2           2.5  
 1/1/2017   d      4   1             4  
 1/1/2017   e      3   3             1  
 1/1/2017   f      0   4             0  
 1/2/2017   a      3   3             1  
 1/2/2017   b      2   2             1  
 1/2/2017   c      3   9   0.333333333  
 1/2/2017   d      4   0             0  
 1/2/2017   e      5   3   1.666666667  
 1/2/2017   f      3   0             0  

this is the code I've written, but the kernel keeps dying (keep in mind this is just an example table, I have about 30,000 rows so nothing too crazy)

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

any idea on what's going on? Is something running infinitely that's causing it to crash? Thanks for the help.

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)

Option 1
You use pd.Series.mask to hide zeros, and then just empty cells with fillna .

v = (df.A / df.B.mask(df.B == 0)).fillna(0)
v

0     0.000000
1     0.000000
2     2.500000
3     4.000000
4     1.000000
5     0.000000
6     1.000000
7     1.000000
8     0.333333
9     0.000000
10    1.666667
11    0.000000
dtype: float64

df['C'] = v

Alternatively, replace those zeros with np.inf , because x / inf = 0 .

df['C'] = (df.A / df.B.mask(df.B == 0, np.inf))

Option 2
Direct replacement with df.replace

df.A / df.B.replace(0, np.inf)

0     0.000000
1     0.000000
2     2.500000
3     4.000000
4     1.000000
5     0.000000
6     1.000000
7     1.000000
8     0.333333
9     0.000000
10    1.666667
11    0.000000
dtype: float64

Keep in mind, you can do an astype conversion, if you want mixed integers and floats as your result:

df.A.div(df.B.replace(0, np.inf)).astype(object)

0            0
1            0
2          2.5
3            4
4            1
5            0
6            1
7            1
8     0.333333
9            0
10     1.66667
11           0
dtype: object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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