python - 基于 Python Pandas 中 DataFrame 中的特征的计算?
问题描述
我有如下数据框:
df = pd.DataFrame({"ID" : ["1", "2", "2", "1", "3"],
"currency" : ["GBP", "GBP", "GBP", "CHF", "EUR"],
"amount" : [100, 200, 300, 400, 500]})
我需要计算:
- New1 = 使用英镑货币的协议数量
- New2 = 与英镑货币的协议金额
我需要如下结果:
解决方案
我们可以过滤groupby
然后reindex
out = df.loc[df.currency=='GBP'].groupby(['ID']).amount.agg(['count','sum']).reindex(df.ID.unique())
Out[210]:
count sum
ID
1 1.0 100.0
2 2.0 500.0
3 NaN NaN
推荐阅读
- php - Laravel 6.16 购物车
- c# - SqlException:INSERT 语句与 FOREIGN KEY 约束冲突数据库表列发生冲突
- python - 字符串 -> ASCII+1 -> 位串
- html - 外部有序列表的样式编号
- masstransit - Masstransit Automatonymous:丢弃丢失 sagas 实例的事件进入错误队列
- azure - Ingress 指标是如何平均的?
- java - java中的static和protected有什么区别?
- java - 使用 Firestore 时如何实现 toObject() 方法泛型?
- kubernetes - 将 kubernetes CNI 从 weave 切换到 calico
- css-selectors - 没有类的元素的 css 选择器