
使用pandas实现excel中countif函数
在pandas中实现类似于excel countif函数的功能非常简单。假如有如下所示的数据集,其中需要统计每行中大于“指标”值的列的个数:
import pandas as pd
data = pd.dataframe({
'x1': [10, 20, 30, 40],
'x2': [15, 25, 35, 45],
'x3': [20, 30, 40, 50],
'x4': [25, 35, 45, 55],
'指标': [18, 28, 38, 48]
})可以通过以下代码实现此功能:
data['countif'] = data.apply(lambda x : sum(data.loc[x.name, 'X1':'X4'] > data.loc[x.name, '指标']), axis =1)
其中:
以上就是如何使用pandas在数据集中统计每行大于“指标”值的列的个数?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号