gpt4 book ai didi

python - 数据框 - 对一些列求和,从其他列中获取最后一个值

转载 作者:行者123 更新时间:2023-11-29 12:24:20 28 4
gpt4 key购买 nike

我得到了一些 Fantasy Football 数据,我正在尝试对其进行整理,以便稍后可以应用 scikit-learn 的全部力量。

我将它保存在 mysql 数据库上,并使用 pd.read_sql 进入数据框。数据框的所有行都是特定球员的比赛,每一列包含红牌、黄牌、助攻、失球等统计数据。这将导致每个球员有多个行,每场比赛都有一个。

我的问题是,如果将其中一些统计数据汇总然后除以上场分钟数,例如进球数、助攻数等,则更有意义。其他的,比如名字、团队、值,只有获取最新的值才有意义。

因此,我想做的是一个新的数据框,其中每个玩家都有一行。某些列将是给定玩家的统计数据总和,而其他列将只是该玩家的最后一个值。

我发现了一种非常丑陋的方法来做到这一点,但总和计算不正确,而且非常困惑。我对 python 还是很陌生,所以感谢所有帮助。最好的方法是什么?

一些数据(只是编出来的,但格式是一样的):

enter image description here

每一行都是给定玩家(本例中为 Szczesny 和 Koscielny)的一场比赛。我想对所有比赛的分钟数、进球数和助攻数等列进行汇总,但其他列(如值和名称)我只想保留最后一个值。

最终结果是这样的:

enter image description here

到目前为止的代码:

import pandas as pd
import mysql.connector

mysql_conn = mysql.connector.connect(user='user', password = 'pass',database='bpl')
#original dataframe
df_playerstats = pd.read_sql('select * from player_stats;', con=mysql_conn)

#index of columns meant to be summed on the original data frame(df_playerstats)
column_sumidx = [3,4,5,6,8,9,10,11,12,13,14,15,16,17,19,23]
#index of columns not meant to be summed
column_nosumidx = [20, 18, 21, 22]

#just the column names I want on my new dataframe
column_names = ['PLAYER_NAME','MINS_PLYD','GOALS_SCORED','ASSISTS','CLEAN_SHEET','OWN_GOALS','PENALTIES_SAVED','PENALTIES_MISSED','YELLOW_CARDS','RED_CARDS','SAVES','BONUS','EA_PPI','BONUS_POINTS_SYS','NET_TRANSFERS','PLAYER_VALUE','POINTS','TEAM_NAME','POSITION','SELECTED_BY']

# this is the new dataframe, the one I wish to fill with one row per player
player_totalstats = pd.DataFrame(index = range(0,no_players),columns = column_names )
# raw dataframe with only the columns meant to be summed
playerstats_sum = df_playerstats.iloc[:,column_sumidx]
# raw dataframe with only the columns not meant to be summed
playerstats_nosum = df_playerstats.iloc[:,column_nosumidx]
for i in range(0,no_players) :
try :
player_totalstats.iloc[i,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,19]] = playerstats_sum[df_playerstats['PLAYER_NAME'] == player_names[i]].sum()

# I use sum with the columns not meant to be summed because I couldn't do it
#otherwise. It works because only one column is summed` `
player_totalstats.iloc[i,[0,15,17,18]] = playerstats_nosum[df_playerstats['PLAYER_NAME'] == player_names[i]][-1:].sum()
except:
print 'oops' , i
break

最佳答案

这是针对您测试数据的解决方案,我认为您可以轻松地将其应用到实际数据中

In [16]: df
Out[16]:
Mins Goals Ass Value Name
0 0 0 0 5.4 Wojciech Szczesny
1 90 0 0 5.4 Wojciech Szczesny
2 0 0 0 5.4 Wojciech Szczesny
3 0 0 0 5.4 Laurent Koscielny
4 90 0 0 5.4 Laurent Koscielny

In [17]: df.groupby('Name').agg({'Mins': np.sum, 'Goals': np.sum, 'Ass': np.sum, 'Value': lambda x: x.iloc[-1]})
Out[17]:
Ass Mins Goals Value
Name
Laurent Koscielny 0 90 0 5.4
Wojciech Szczesny 0 90 0 5.4

关于python - 数据框 - 对一些列求和,从其他列中获取最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28567156/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com