How To Create Multiple Data Frames From Another Dataframe In A Loop
iam new in python and wanna create multiple data frames from another dataframe in a loop. i have a dataframe like below and want to create dataframes for each period and want to pu
Solution 1:
Create a dict with 3 entries where the key is the period and the value is the corresponding subset dataframe:
dfs = dict(list(df.groupby('period')))
>>> dfs[1167]
id period
011167121167>>> dfs[1168]
id period
231168341168>>> dfs[1169]
id period
451169561169
Don't use this
If you really want to create 3 variables df1167
, df1168
and df1169
that can be direct accessible by their name:
for period, subdf in df.groupby('period'):
locals()[f'df_{period}'] = subdf
>>> df_1167
id period
011167121167>>> df_1168
id period
231168341168>>> df_1169
id period
451169561169
Post a Comment for "How To Create Multiple Data Frames From Another Dataframe In A Loop"