【问题标题】:Formatting a Dataframe into a table stored as PNG/JPEG将 Dataframe 格式化为存储为 PNG/JPEG 的表
【发布时间】:2020-03-22 20:36:20
【问题描述】:

我有一个 DataFrame,我想将它作为 PNG/JPEG 粘贴到 Powerpoint 中。 我希望表格有一些格式。我一直在尝试使用 Matplotlib,并且有计划地进行,但我并没有接近最终结果。

我试过了:

import plotly.graph_objects as go
import pandas as pd

df = full_table
header_color = '#1D97FF'

fig = go.Figure(data=[go.Table(

    header=dict(values=list(df.columns),
                fill_color=header_color,
                align='left', 
                line_color='darkslategray', 
                font=dict(color='white', size=11)),

    cells=dict(values=[df.index, df.budget_revenue, df.budget_net_revenue, df.gross_margin,df.new_net_mrr,df.actual_shipments],
               fill_color=['white'],
               align='left', line_color='darkslategray', font=dict(color='black', size=11)))                   
])

fig.write_image("fig1.png")
fig.show()

我已经试过了:

import matplotlib.pyplot as plt
# Prepare table

df = full_table
hc = '#1D97FF'

columns=full_table.columns.to_list()
index=full_table.index.to_list()
values=full_table.values.tolist()
# Add a table at the bottom of the axes
colors = [["#56b5fd","w","w","w","w","w","w"],[ "#1ac3f5","w","w","w","w","w","w"]]


# Add a table at the bottom of the axes
colors = [["w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
         [ "w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
         [ "w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
         [ "w","w","w","w","w","w"],[ hc,hc,hc,hc,hc,hc]]
colors1 =  ["w","w","w","w","w","w","w",hc]
colors2 = [ hc,hc,hc,hc,hc,hc]

fig, ax = plt.subplots()

ax.axis('off')
the_table = ax.table(cellText=values,cellColours=colors,
                     colLabels=columns,colColours=colors2,rowLabels=index, rowColours=colors1,loc='center')

plt.show()

第一个选项看起来已经很不错了。一些细微的变化,但我不能让底行变成蓝色。并且列标题不适合。 第二个选项是模糊的,左上角仍然是空的,应该做更多的字体编辑,我也没有成功。

我只是使用了不正确的库。坚持使用 Excel 在一分钟内完成的一些简单格式设置。

它应该是这样的:

【问题讨论】:

  • [['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20 ', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], [ 'Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 82854.06800000001, 82854.06800000001, nan, 250000.0]] ['NL','DE','AT','FR','BE','ES','IT','全球'] ['月','budget_revenue','budget_net_revenue',' Gross_margin'、'new_net_mrr'、'actual_shipments']
  • 请提供数据框。谢谢。
  • 数据 = [['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan -20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0] , ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 82854.06800000001, 82854,068000000, 0, 250000.0]] 索引 = ['NL', 'DE', 'AT', 'FR', 'BE', 'ES', 'IT', 'Global'] 列 = ['Months', 'budget_revenue' , 'budget_net_revenue', 'gross_margin', 'new_net_mrr', 'actual_shipments'] df = pd.DataFrame(data, index=index, columns=columns)
  • 现在看起来很乱,将pandas作为pd导入,数据框就出来了。数值与上图略有不同。
  • 我只需要那种格式。我想知道对数据框进行任何格式化的最佳方法是什么..

标签: python pandas matplotlib html-table plotly


【解决方案1】:

你应该把你的表格分成三个逻辑部分:

  1. 标题
  2. “中心”内容
  3. 最后一行

这是代码:

import plotly.graph_objects as go
import pandas as pd

data = [['Jan-20', 1.0, 1, 4.0, 0, 250000.0],
        ['Jan-20', 2.0, 2, 4.0, 0, 250000.0],
        ['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
        ['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
        ['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
        ['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
        ['Jan-20', 4.0, 4, 4.0, 0, 250000.0],
        ['Jan-20', 5, 232, 114, 2312, 123]]
index = ['NL', 'DE', 'AT', 'FR', 'BE', 'ES', 'IT', 'Global']
columns = ['Months', 'budget_rev', 'budget_net_rev', 'gross_margin', 'new_net_mrr', 'act_shipments']
df = pd.DataFrame(data, index=index, columns=columns) 

df.reset_index(inplace=True)
n_cols = len(columns)
n_rows = len(data)

header_c = 'dodgerblue'
rows_c = ['white']*(n_rows-1)

rows_c.extend(['dodgerblue'])

rows_f = ['darkslategray']*(n_rows-1)
rows_f.extend(['white'])

fig = go.Figure(data=[go.Table(columnwidth = [80]*(n_cols+1),
                               header=dict(values=list(df.columns),
                                           line_color='darkslategray',
                                           fill_color=header_c,
                                           font=dict(color='white',
                                                     size=12)),
                               cells=dict(values=list(df.values.transpose()),
                                          line_color='darkslategray',
                                          fill_color = [rows_c*n_cols],
                                          font = dict(color = [rows_f*n_cols],
                                                      size = 12)))])

fig.show()

你会得到:

【讨论】:

  • 太棒了。我可以使用它。非常感谢男人!
猜你喜欢
  • 2021-04-19
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 2011-01-18
相关资源
最近更新 更多