0

I have grouped a DataFrame using data.groupby('column) and now I want to create a dataframe from each group:

for i in data_group.indices:
    i = data_group.get_group(i)

I can print the dataframes out within the for-loop, but I can't access them otherwise... Somehow the naming of the DataFrame with a variable is not working. Does anyone have a solution?

3
  • 1
    As you iterate, store them in a list. If they need names, store them in a dictionary and use the names as keys. Commented Feb 3, 2018 at 16:03
  • Why do you need them assigned to variables? Can you just use .get_group('whatever') when you need it? Commented Feb 3, 2018 at 16:06
  • 1
    Possible duplicate of Using a loop in Python to name variables Commented Feb 3, 2018 at 16:07

1 Answer 1

1

You can store them in a list

g=data.groupby('column') 
l=[]
for x,df in g : 
    l.append(df)

Or using get_group

g.get_group('groupkey')
Sign up to request clarification or add additional context in comments.

1 Comment

Even though this does not yield individual dataframes, but one dictionary with key-value pairs I can access them from outside the for-Loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.