python - 求和函数问题
问题描述
我现在不擅长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'
我正在从源代码编写此代码,但是这个人没有错误警报,但我有。我能做些什么?谢谢。
解决方案
使用pd.Series.sum
和检查阈值pd.Series.gt
。然后我们使用np.where
get high
orlow
代替True
or False
in np.array
that 我们分配给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
推荐阅读
- php - 在搜索栏中以法语格式化日期
- c++ - 计算表示为字符串的 2 个大数之间的模数
- swift - 如何通过 Swift 映射来自 Firestore 的文档中的嵌套数据?
- r - 在模型中使用具有数百个级别的因子
- php - 选择查询中的 Laravel 传递列表参数(列)
- python-3.x - How to return string composed of superscript inside of fraction for matplotlib legend label?
- c - C 中哪些运算符的优先级错误?
- c# - 为什么我的 .net 核心 API 取消请求?
- r - 检查下载的 jpg 的矢量并找到丢失的 id
- c# - @Before 方法调用的用法