简体   繁体   English

Python Pandas数据透视表-计数点

[英]Python Pandas Pivot Table - counting points

I have an issue with Pivot table in Python. 我在Python中有数据透视表问题。 Let's say that I have below values in list: 假设我在列表中有以下值:

team_A_id = [1,5,10] team_A_id = [1,5,10]

team_A_result = 0 team_A_result = 0

and below data frame: 及以下数据框:

id             points
3                36
4                0
5                11
7                6
10               23

How could I using (perhaps) "for loop" find by team A id in list points and count them. 如何使用(也许)“ for循环”按团队A在列表点中找到ID并进行计数。 Output should be: 输出应为:

result_team_A = 34 result_team_A = 34

Thanks for any help 谢谢你的帮助

You are looking for isin and sum 您正在寻找isinsum

team_A_id = [1,5,10]
df.loc[df.id.isin(team_A_id),'points'].sum()
Out[136]: 34

this will return the rows for the team A: 这将返回团队A的行:

df.iloc[team_A_id]

result team A can be obtained by: 可以通过以下方式获得结果小组A:

df['points].sum()

TLDR: TLDR:

df.iloc[team_A_id]['points].sum()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM