Python Pandas - 将一列除以另一列

31

我正在尝试向我的DataFrame中添加一列,该列是另外两列之商的乘积,代码如下:

df['$/hour'] = df['$']/df['hours']

这个方法运行良好,但是如果在['hours']中的值小于1,那么['$/hour']的值将大于['$']的值,这不是我想要的。

是否有一种控制操作的方式,使得如果['hours'] < 1,则 df['$/hour'] = df['$']

4个回答

16

您可以使用 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

8
df['$/hour'] = df.apply(lambda x: x['$'] if x['$'] < 1 else x['$']/x['hours'], axis=1)

6

您还可以使用 DataFrame.loc 进行索引过滤和选择:

df['$/hour'].loc[df['hours']>=1] = df['$']/df['hours']
df['$/hour'].loc[df['hours']<1] = df['$']

如果列$/hour事先不存在,这将引发KeyError。我建议使用df.loc[df['hours']>=1, '$/hour'] = df['$']/df['hours']df.loc[df['hours']<1, '$/hour'] = df['$'] - rachwa

0

你也可以使用mask

df['$/hour'] = (df['$'] / df['hours']).mask(df['hours'] < 1, df['$'])

如果满足条件 df['hours'] < 1,则取列 $ 的值,否则将 $ 除以 hours

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接