-1

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 put period id as a name for each created dataframes.

id      period
1       1167
2       1167
3       1168
4       1168 
5       1169
6       1169
...

i have tried something like below

for i in KAI_stores['period']: i=pd.DataFrame()

in the end i want to have

first df named as 1167
id    period
1     1167
2     1167

second df named as 1168
id    period 
3     1168
4     1168
and ... for each period
2
  • This is not necessarily the best strategy. What is your use case for creating multiple data frames rather than applying calculations to the "period" group? Commented Jul 6, 2021 at 9:20
  • i did t get what you mean but suppose i have 10 periods in the first dataframe and in the end i want to have 1167 id period 1 1167 2 1167 1168 id period 3 1168 4 1168 ... Commented Jul 6, 2021 at 9:31

1 Answer 1

0

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
0   1    1167
1   2    1167

>>> dfs[1168]
   id  period
2   3    1168
3   4    1168

>>> dfs[1169]
   id  period
4   5    1169
5   6    1169

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
0   1    1167
1   2    1167

>>> df_1168
   id  period
2   3    1168
3   4    1168

>>> df_1169
   id  period
4   5    1169
5   6    1169
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.