英文:
Python: Divide two columns, a/b, if b is 0 -> assign 0
问题
需要在数据框(df)中创建一个新列,这个新列是将两列(apples和pupils)相除,如果pupils的数量为0,那么该行应该被赋值为零。
当前数值:
apples | pupils |
---|---|
5 | 2 |
6 | 3 |
5 | 0 |
预期输出:
apples | pupils | avg_apples_per_pupils |
---|---|---|
5 | 2 | 2.5 |
6 | 3 | 2.0 |
5 | 0 | 0.0 |
我尝试使用以下代码:
if df[df['pupils'] != 0]:
df['avg_apples_per_pupils'] = df['apples'] / df['pupils']
else:
df['avg_apples_per_pupils'] = df.assign(avg_apples_per_pupils='0')
但是我遇到了以下错误:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我完全可以考虑其他方法。
英文:
I need to make new column in df (apples per pupils) divide two columns (apples and pupils), and if the number of pupils is 0, I should just assign zero to that row.
Current values:
apples | pupils |
---|---|
5 | 2 |
6 | 3 |
5 | 0 |
Expected output:
apples | pupils | avg_apples_per_pupils |
---|---|---|
5 | 2 | 2.5 |
6 | 3 | 2.0 |
5 | 0 | 0.0 |
I tried using this:
if df[df['pupils']!=0]:
df['avg_apples_per_pupils'] = df['apples'] / df['pupils']
else:
df['avg_apples_per_pupils'] = df.assign(avg_apples_per_pupils='0')
but I get this error:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I'm completely open for other approaches
答案1
得分: 3
你可以使用mask
函数:
df['avg_apples_per_pupils'] = (df['apples'].div(df['pupils'])
.mask(df['pupils'].eq(0), 0)
)
或者使用numpy.divide
:
df['avg_apples_per_pupils'] = np.divide(df['apples'], df['pupils'],
where=df['pupils'].ne(0))
输出:
apples pupils avg_apples_per_pupils
0 5 2 2.5
1 6 3 2.0
2 5 0 0.0
英文:
You can use a mask
:
df['avg_apples_per_pupils'] = (df['apples'].div(df['pupils'])
.mask(df['pupils'].eq(0), 0)
)
Or numpy.divide
:
df['avg_apples_per_pupils'] = np.divide(df['apples'], df['pupils'],
where=df['pupils'].ne(0))
Output:
apples pupils avg_apples_per_pupils
0 5 2 2.5
1 6 3 2.0
2 5 0 0.0
答案2
得分: 2
使用 np.where
条件:
df['avg_apples_per_pupils'] = np.where(df['pupils'].ne(0), df['apples'] / df['pupils'], df['pupils'])
apples pupils avg_apples_per_pupils
0 5 2 2.5
1 6 3 2.0
2 5 0 0.0
英文:
Use np.where
condition:
df['avg_apples_per_pupils'] = np.where(df['pupils'].ne(0), df['apples'] / df['pupils'], df['pupils'])
apples pupils avg_apples_per_pupils
0 5 2 2.5
1 6 3 2.0
2 5 0 0.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论