Pandas: How to reference and print multiple dataframes as HTML tables

前端 未结 1 1121
傲寒
傲寒 2021-01-04 06:14

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

相关标签:
1条回答
  • 2021-01-04 06:48

    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

    0 讨论(0)
提交回复
热议问题