I\'m trying split out individual dataframes from a groupby
to print them as pandas HTML tables. I need to reference and render them individually as tables so I
As Thomas K points out, you could use IPython.core.display.display
to incorporate the display of DataFrames along with print statements in an IPython notebook:
import pandas as pd
from IPython.core import display as ICD
df = pd.DataFrame(
{'area': [5, 42, 20, 20, 43, 78, 89, 30, 46, 78],
'cost': [52300, 52000, 25000, 61600, 43000, 23400, 52300, 62000, 62000, 73000],
'grade': [1, 3, 2, 1, 2, 2, 2, 4, 1, 2], 'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005],
'team': ['man utd', 'chelsea', 'arsenal', 'man utd', 'man utd', 'arsenal', 'man utd', 'chelsea', 'arsenal', 'arsenal']})
result = df.groupby(['team', 'grade']).agg({'cost':'mean', 'area':'mean', 'size':'sum'}).rename(columns={'cost':'mean_cost', 'area':'mean_area'})
dfs = {team:grp.drop('team', axis=1)
for team, grp in result.reset_index().groupby('team')}
for team, grp in dfs.items():
print(team)
ICD.display(grp)
generates