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?

illright
  • 3,991
  • 2
  • 29
  • 54
maerzm
  • 1
  • 1
    As you iterate, store them in a list. If they need names, store them in a dictionary and use the names as keys. – wwii Feb 03 '18 at 16:03
  • Why do you need them assigned to variables? Can you just use `.get_group('whatever')` when you need it? – wwii Feb 03 '18 at 16:06
  • 1
    Possible duplicate of [Using a loop in Python to name variables](https://stackoverflow.com/questions/13603215/using-a-loop-in-python-to-name-variables) – wwii Feb 03 '18 at 16:07

1 Answers1

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')
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Even though this does not yield individual dataframes, but one dictionary with key-value pairs I can access them from outside the for-Loop. – maerzm Feb 04 '18 at 09:02