Pandas:如何将多个数据帧引用和打印为 HTML 表格 [英] Pandas: How to reference and print multiple dataframes as HTML tables

查看:49
本文介绍了Pandas:如何将多个数据帧引用和打印为 HTML 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 groupby 中分离出单个数据帧,以将它们打印为 Pandas HTML 表.我需要将它们单独引用并呈现为表格,以便我可以对它们进行屏幕截图以进行演示.

这是我当前的代码:

将pandas导入为pddf = pd.DataFrame({'区域': [5, 42, 20, 20, 43, 78, 89, 30, 46, 78],'成本': [52300, 52000, 25000, 61600, 43000, 23400, 52300, 62000, 62000, 73000],等级":[1, 3, 2, 1, 2, 2, 2, 4, 1, 2],尺寸":[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('团队',axis=1)对于团队, grp 在 result.reset_index().groupby('team')}对于团队,dfs.items() 中的 grp:打印('{}:
{}
'.format(team, gap))

打印(作为非 HTML 表格):

切尔西:等级 mean_cost mean_area 大小2 3 52000 42 9573 4 62000 30 849兵工厂:等级 mean_cost mean_area 大小0 1 62000.000000 46.000000 9731 2 40466.666667 58.666667 3110曼联:等级 mean_cost mean_area 大小4 1 56950 12.5 24455 2 47650 66.0 2579

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

解决方案

As

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 can screenshot them for a presentation.

This is my current code:

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('{}:
{}
'.format(team, gap))

Which prints (as non HTML tables):

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

Is it possible to get these dataframes one by one as HTML tables? For the avoidance of doubt, I don't need an iterative method to return them all as HTML tables in one go - am happy to reference each one individually.

解决方案

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

这篇关于Pandas:如何将多个数据帧引用和打印为 HTML 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆