Reputation: 109
I have a master Dataframe from which I want to create new DataFrame based on unique value of column values. So, anything with a value 'x' in a particular column will be added to a DataFrame of its own. And, I want to create it dynamically, while naming each of the newly created DataFrame uniquely. Can anyone tell how I could do that?
For example, In the picture, the rows with the "Team ID" = 7514332
will be a new DataFrame with name "P_1"
I wrote this for the DataFrame:
p_1 = player_df.loc[player_df['Team ID'].isin([7514332])]
But, here I hard coded the Team ID to 7514332 I have the Team ID sitting in a column in another DF Is there I can Iterate over them to match the Team ID and create them dynamically, instead of hardcoding them?
For instance, the code would match the Team ID for 7506093 from the other DF and create "P_2"
with the rows for only that particular Team ID
And this would repeat for "P_3"
, "P_4"
etc.
Upvotes: 1
Views: 159
Reputation: 720
you can create list of df which include [p_1,p_2,..]
by this:
p_df = [player_df.loc[player_df['Team ID'].isin([item])] for item in set(player_df['Team ID'].values) ]
Upvotes: 2