[英]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. 我有一个包含日期,项目和2个值的pandas数据框。 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. 我要做的就是输出另一列,如果列B大于0则是列A /列B的乘积,如果列B等于0,则输出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) 这是我写的代码,但是内核一直在死(请记住这只是一个示例表,我有大约30,000行,所以没什么太疯狂的)
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
你可以使用np.where
获得它
df['C'] = np.round(np.where(df['B'] > 0, df['A']/df['B'], 0), 1)
Or if you want to use loc
或者如果你想使用loc
df.loc[df['B'] > 0, 'C'] = df['A']/df['B']
and then fillna(0)
然后fillna(0)
Option 1 选项1
You use pd.Series.mask
to hide zeros, and then just empty cells with fillna
. 使用pd.Series.mask
隐藏零,然后使用pd.Series.mask
清空单元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
. 或者,用np.inf
替换那些零,因为x / inf = 0
。
df['C'] = (df.A / df.B.mask(df.B == 0, np.inf))
Option 2 选项2
Direct replacement with df.replace
用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: 请记住,如果你想要混合整数和浮点数,你可以做一个astype
转换:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.