Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

python - How to combine rows in a pandas dataframe that have the same value in one column

I have a pandas dataframe of NBA player stats from the 2019-2020 season. Some players names show up more than once because they played on multiple different teams throughout the season. I want to organize the dataframe so that each player's name only appears once, and for the players whose names appear more than once, I want to take the average of all their stats and put it into one row.

For example, if there was a player that played on 3 different teams and appeared in 3 consecutive rows, I want to combine those 3 rows into one row, with that new row being the average of all the stats for the three rows.

Here is an example of player names appearing multiple times:

example

Is there any simple way to do this? I don't know how many times a player might appear, and I don't know how many players' names appear multiple times. I want to iterate through the dataframe and take the average of all stats for rows that have the same player name.

If needed, I can delete the 'Tm' column, or any of the string columns really (besides 'Player') since I don't absolutely need those, but I'd rather keep them if possible.

question from:https://stackoverflow.com/questions/65848929/how-to-combine-rows-in-a-pandas-dataframe-that-have-the-same-value-in-one-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use group by method for this:

cols = [col for col in df.columns if all(char.isdigit() for char in col)]
df.groupby('player')[cols].mean()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...