3

I have a data frame which has been grouped by 'hour'. I want to use a for loop which loops through each group and creates a data frame for each group. The code I'm using at the moment only creates a data frame for the last group that it iterates through. Any suggestions on how I can get it to work properly?

for name, group in data.groupby('hour'):
    d = {'group_' + str(name) : group}

1 Answer 1

11

You're overwriting your object each time you want this:

d = {}
for name, group in data.groupby('hour'):
    d['group_' + str(name)] = group
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my goodness, what a stupid mistake. Thanks very much. Works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.