“`python Python: 如果 b 为 0,则将两列 a/b 进行相除 -> 赋值为 0 “`

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月24日 02:57:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定