简体   繁体   English

python pandas - 将列除以另一列

[英]python pandas - dividing column by another column

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.这很好用,但如果['hours']中的值小于1 ,则['$/hour']值大于['$']中的值,这不是我想要的。

Is there a way of controlling the operation so that if ['hours'] < 1 then df['$/hour'] = df['$'] ?有没有办法控制操作,如果['hours'] < 1 then df['$/hour'] = df['$']

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
df['$/hour'] = df.apply(lambda x: x['$'] if x['$'] < 1 else x['$']/x['hours'], axis=1)

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['$']

You can also use mask :您还可以使用mask

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

If the condition df['hours'] < 1 is met the values from column $ are taken, otherwise $ is divided by hours .如果满足条件df['hours'] < 1 ,则采用$列中的值,否则$除以hours

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 中将一列与另一列分开在 pandas python 3 中给出错误的结果 - Dividing one column with another in python is giving wrong result in pandas python 3 Pandas-将列除以另一列,条件是值是否大于0? - Pandas- Dividing a column by another column conditional on if values are greater than 0? 在 Python/R 中将表与其另一列分开 - Dividing a table with its another column in Python/ R 将pandas数据框的特定列的所有值相加,然后用总和除以索引(pandas,python)-在数据帧中添加另一个列 - Adding all values for a particular column of a pandas data frame and dividing by sum by index (pandas, python) - ADDING AS ANOTHER COLUMN IN DATAFRAME Python Pandas - 如果使用 groupby 函数汇总了两列中的数据,则将其中的数据除以另一列 - Python Pandas - Dividing data in one column by another if both are summarized using the groupby function 根据python另一列中的公共值在数据框的一列中划分2个值 - Dividing 2 values in a column of dataframe based on common value in another column in python 如何绘制用熊猫除以一列得到的5个最大值? - How to plot 5 largest values obtained by dividing one column by another with pandas? 按年份划分 Pandas 列 - Dividing Pandas column based on year 熊猫除以多个列条目 - Pandas dividing by multiple column entries pandas将列除以滞后值 - pandas dividing a column by lagged values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM