首页 > 解决方案 > 求和函数问题

问题描述

我现在不擅长python,我已经尝试了很长时间但我做不到,我想对列中的值求和,但我遇到了这样的错误:

<lambda>() missing 2 required positional arguments: 'y' and 'z'

这些是代码:

threshold = sum(data2.budget)/len(data2.budget)
print(threshold)
data2["budget_level"] = ["high" if i > threshold else "low" for i in data2.budget]
data2.loc[:10,["budget_level","budget"]]

这都是警报:

TypeError                                 Traceback (most recent call last)
<ipython-input-240-f1303a50f5b0> in <module>
----> 1 threshold = sum(data2.budget)/len(data2.budget)
      2 print(threshold)
      3 data2["budget_level"] = ["high" if i > threshold else "low" for i in data.budget]
      4 data2.loc[:10,["budget_level","budget"]]

TypeError: <lambda>() missing 2 required positional arguments: 'y' and 'z'

我正在从源代码编写此代码,但是这个人没有错误警报,但我有。我能做些什么?谢谢。

标签: pythonpandas

解决方案


使用pd.Series.sum和检查阈值pd.Series.gt。然后我们使用np.whereget highorlow代替Trueor Falsein np.arraythat 我们分配给data2["budget_level"]

threshold = data2['budget'].sum()/len(data2)

data2["budget_level"] = np.where(data2['budget'].gt(threshold),'high','low')
# if you are really checking data and not data2
#data2["budget_level"] = np.where(data['budget'].gt(threshold),'high','low')

或者

data2["budget_level"] = data2['budget'].gt(threshold).map({True:'high',False:'low'})

例子

df = pd.DataFrame({'A':[1,2,3,4,5]})
import numpy as np
threshold = 2
df['B'] = np.where(df['A'].gt(threshold),'high','low')
print(df)
   A     B
0  1   low
1  2   low
2  3  high
3  4  high
4  5  high

推荐阅读