python pandas - 将列除以另一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35439613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas - dividing column by another column
提问by Charon
I'm trying to add a column to my DataFrame
which is the product of division of two other columns, like so:
我正在尝试向我的列添加一列,DataFrame
它是其他两列除法的乘积,如下所示:
df['$/hour'] = df['$']/df['hours']
This works fine, but if the value in ['hours']
is less than 1
, then the ['$/hour']
value is greater than the value in ['$']
, which is not what I want.
这工作正常,但是如果 in 的值['hours']
小于1
,则该['$/hour']
值大于 中的值['$']
,这不是我想要的。
Is there a way of controlling the operation so that if ['hours'] < 1
then df['$/hour'] = df['$']
?
有没有办法控制操作,如果['hours'] < 1
那么df['$/hour'] = df['$']
?
采纳答案by jezrael
You can use numpy.where
:
您可以使用numpy.where
:
print df
hours $
0 0 8
1 0 9
2 0 9
3 3 6
4 6 4
5 3 7
6 5 5
7 10 1
8 9 3
9 3 6
10 5 4
11 5 7
df['$/hour'] = np.where(df['hours'] < 1, df['hours'], df['$']/df['hours'])
print df
hours $ $/hour
0 0 8 0.000000
1 0 9 0.000000
2 0 9 0.000000
3 3 6 2.000000
4 6 4 0.666667
5 3 7 2.333333
6 5 5 1.000000
7 10 1 0.100000
8 9 3 0.333333
9 3 6 2.000000
10 5 4 0.800000
11 5 7 1.400000
回答by Brian Huey
df['$/hour'] = df.apply(lambda x: x['$'] if x['$'] < 1 else x['$']/x['hours'], axis=1)
回答by agold
You can also filter and select the indexes to set with DataFrame.loc
:
您还可以过滤和选择要设置的索引DataFrame.loc
:
df['$/hour'].loc[df['hours']>=1] = df['$']/df['hours']
df['$/hour'].loc[df['hours']<1] = df['$']