Skip to content
Advertisement

Loop through multiple small Pandas dataframes and create summary dataframes based on a single column

I have a bunch of small dataframes each representing a single match in a game. I would like to take these dataframes and consolidate them into a single dataframe for each player without knowing the player’s names ahead of time.

The starting dataframes look like this:

JavaScript

And I would like to get to a series of frames looking like this

JavaScript

My problem is that the solutions that I’ve found so far all require me to know the player names ahead of time and manually set up a dataframe for each player. Since I’ll be working with 40-50 players and I won’t know all their names until I have the raw data I’d like to avoid that if at all possible.

I have a loose plan to create a dictionary of players with each player key containing a dict of their rows from the dataframes. Once all the match dataframes are processed I would convert the dict of dicts into individual player dataframes. I’m not sure if this is the best approach though and am hoping that there’s a more efficient way to do this.

Advertisement

Answer

Let’s try concat + groupby then build out a dict:

JavaScript

dfs:

JavaScript

Each player’s DataFrame can then be accessed like:

dfs['player1']:

JavaScript

Or as a list:

JavaScript

dfs:

JavaScript

Each player’s DataFrame can then be accessed like:

dfs[1]:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement