数据帧组和排序

2024-09-27 21:32:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大致如下的数据帧:
enter image description here

我可以对它们进行分组和分类 df_players.groupby(['vehicle']).agg({'frags':sum}).sort_values(['frags'],ascending=False)
enter image description here
我如何对vehicles进行分组,并像这样获得他们的获胜率:
enter image description here

编辑:(数据)

[
    {"player": "bob", "vehicle": "a", "hasWon": True, "frags": 5},
    {"player": "foo", "vehicle": "b", "hasWon": False, "frags": 3},
    {"player": "bar", "vehicle": "c", "hasWon": True, "frags": 2}
]

Tags: 数据falsetruedf分类sortaggplayer
3条回答

解决方案

# Assuming 
#     winrate = sum(hasWon*frags)/sum(frags)
# df['winrate'] = df['hasWon']*df['frags']/df['frags'].sum()

# Calculate Aggregate Result
result = (df.groupby(['vehicle'])
            .agg({
                'frags': sum, 
                'winrate': sum
            })

enter image description here

虚拟数据

import numpy as np
import pandas as pd

df = pd.DataFrame({'player': ['bob', 'foo', 'bar', 'foo2', 'bar2'], 
                   'vehicle': list('abcab'), 
                   'hasWon': [True, False, True, True, True], 
                   'frags': [5, 3, 2, 4, 2]})
# Assuming 
#     winrate = sum(hasWon*frags)/sum(frags)
df['winrate'] = df['hasWon']*df['frags']/df['frags'].sum()

df

enter image description here

我认为您需要对hasWon列的mean进行聚合,因为True1一样进行处理,然后按100进行倍数,四舍五入,最后转换为列:

df_players = pd.DataFrame({'vehicle': list('aabbccc'), 
                           'hasWon':[True, False, True, True, False, True, False], 
                           'frags':[5,2,3,6,5,4,5]})
print (df_players)
  vehicle  hasWon  frags
0       a    True      5
1       a   False      2
2       b    True      3
3       b    True      6
4       c   False      5
5       c    True      4
6       c   False      5

df = (df_players.groupby(['vehicle'])['hasWon']
                 .mean()
                 .mul(100)
                 .round(2)
                 .reset_index(name='winrate'))
print (df)
  vehicle  winrate
0       a    50.00
1       b   100.00
2       c    33.33

我将只分配一个新列,以便只使用mean

df.assign(winrate=np.where(df['hasWon'], 100, 0)).groupby('vehicle').agg({'winrate': 'mean'})

相关问题 更多 >

    热门问题