熊猫:如何将多个数据帧作为HTML表格引用和打印

RDJ*_*RDJ 12 python ipython pandas jupyter-notebook

我正在尝试从a分割出单个数据帧,groupby将它们打印为pandas HTML表格.我需要将它们作为表单独引用和呈现,以便我可以截取它们进行演示.

这是我目前的代码:

import pandas as pd

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('{}:\n{}\n'.format(team, gap))
Run Code Online (Sandbox Code Playgroud)

哪些打印(作为非HTML表格):

chelsea:
   grade  mean_cost  mean_area  size
2      3      52000         42   957
3      4      62000         30   849

arsenal:
   grade     mean_cost  mean_area  size
0      1  62000.000000  46.000000   973
1      2  40466.666667  58.666667  3110

man utd:
   grade  mean_cost  mean_area  size
4      1      56950       12.5  2445
5      2      47650       66.0  2579
Run Code Online (Sandbox Code Playgroud)

是否可以将这些数据帧逐个作为HTML表格获取?为了避免疑问,我不需要迭代方法将它们全部作为HTML表格返回 - 我很乐意单独引用每个表格.

unu*_*tbu 13

正如Thomas K指出的那样,您可以使用IPython.core.display.display在IPython笔记本中合并DataFrames的显示以及print语句:

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)
Run Code Online (Sandbox Code Playgroud)

生成
在此输入图像描述

  • 你实际上有一个不必要的步骤 - 你应该能够在末尾调用`ICD.display(grp)`来显示数据帧的丰富repr. (2认同)