'Groupby a single column and split into different dataframes

I have a dataframe (shown below)that has a owner column and I want to groupby owner and save those into different dataframes ( the end goal is to convert df to html tables). Below is the solution I have tried.

Person Number City
John 849732 atlanta
Ali 87543 denver
John 873543 knoxville
Ali 898779 aspen
Sam 875435 nashville

I want to create separate dfs for John, Ali and Sam.

res = dict(tuple(df.groupby('Person')))
listing = list (res)
for l in listing:
   new_df = res[l]
   print(new_df)

My problem is I end up with a list of data frames after doing this and I am having trouble into splitting them into separate data frames that can be converted to html files. Perhaps something like this might work but I am missing some logic because I only get one df instead of looping through and getting multiples.

 for owner in new_df:
        html = df.to_html()


Solution 1:[1]

There are a couple of approaches to this. The documentation might help.

The GroupBy object returned by groupby() supports iteration. It produces tuples of the form (name, subset) where subset here is each dataframe you are looking for. So your code could just look something like:

html_by_owner = {}
for owner, new_df in df.groupby('Person'):
    html_by_owner[owner] = new_df.to_html()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeff