【问题标题】:Python Create Tables and save as CSV and show 1st row contentsPython 创建表并保存为 CSV 并显示第一行内容
【发布时间】:2018-07-24 13:48:56
【问题描述】:

我有这个功能码:

def make_dash_table(df):
    table = []
    for index, row in df.iterrows():
        html_row = []
        for i in range(len(row)):
            html_row.append(html.Td([row[i]]))
        table.append(html.Tr(html_row))
    return table

它为 Dash Framework (Python) 创建了一个表,但是当我在 Python 上调用它时它不显示标题(第一个 .csv 行)。对于某些表格,它被定制为显示如下字幕:

modifed_perf_table.insert(
    0, html.Tr([
        html.Td([]),
        html.Td(['Cumulative'], colSpan=4, style={'text-align': "center"}),
        html.Td(['Annualised'], colSpan=4, style={'text-align': "center"})
    ], style={'background': 'white', 'font-weight': '600'}
    )
)

但我想让表格显示 csv 标题(第一行)。 我需要更改的地方:是在此代码上还是在 CSS 上?

完整代码如下:

df_perf_summary = pd.read_csv('17530.csv', encoding='latin-1')
df_perf_summary.head()

def make_dash_table(df):

    table = []
    for index, row in df.iterrows():
        html_row = []
        for i in range(len(row)):
            html_row.append(html.Td([row[i]]))
        table.append(html.Tr(html_row))
    return table


modifed_perf_table = make_dash_table(df_perf_summary)


modifed_perf_table.insert(
    0, html.Tr([
        html.Td([]),
        html.Td(['Cumulative'], colSpan=4, style={'text-align': "center"}),
        html.Td(['Annualised'], colSpan=4, style={'text-align': "center"})
    ], style={'background': 'white', 'font-weight': '600'}
    )
)

 html.Div([

            html.Div([

            html.Div([
                html.H6("#####",
                        className="gs-header gs-table-header padded"),
                html.Table(modifed_perf_table, className="reversed"),

            ], className="eight columns"),

        ], className="row "),

external_css = ["https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css",
            "https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css",
            "//fonts.googleapis.com/css?family=Raleway:400,300,600",
            "https://cdn.rawgit.com/plotly/dash-app-stylesheets/5047eb29e4afe01b45b27b1d2f7deda2a942311a/goldman-sachs-report.css",
            "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"]

csv如下,另存为17530.csv

Col1,Since Launch,,March,April,May,1st Y,2nd Y,3rd Y
,Cum (#),Cum (%),Q2Y18,Q3Y18,Q4Y18,1stYJun19,2ndYJun20,3rdYJun2021
SiteA,15.96,,0.1,0.27,0.27,0.87,0.51,0.43
SiteB,20.09,,0.06,0.21,0.21,2.24,'-1.48,1.46
SiteC,15.7,,'-0.03,'-0.09,'-0.09,'-0.32,'-0.09,0.04

【问题讨论】:

  • 你能分享在CSV文件中读取的代码来制作df吗?
  • @PaSTE,在帖子中添加了完整代码,请检查!谢谢
  • 看起来解决方案可以来自这部分 CSS (cdn.rawgit.com/plotly/dash-app-stylesheets/…) table.tiny-header tr:first-child{ font-size: 8px; } .columns{ 左边距:0 !important; } .row > .columns:not(:first-child){ padding-left: 20px; }

标签: python pandas plotly-dash


【解决方案1】:

make_dash_table 函数不打印列标签,因为列标签不包含在 DataFrame 对象的行中。列标签可以通过DataFrame.columns 成员访问,该成员通常是python 字符串的Series。要将它们添加为 HTML 表中的第一行,请在遍历行之前对其进行处理:

def make_dash_table(df):
    html_row = [html.Td([col]) for col in df.columns]
    table = [html.Tr(html_row)]
    for index, row in df.iterrows():
        html_row = []
        for i in range(len(row)):
            html_row.append(html.Td([row[i]]))
        table.append(html.Tr(html_row))
    return table

【讨论】:

  • 非常感谢您快速解决问题。问候。
  • 是否可以隐藏索引标题(Col1)?另外,在某些pivot_table中出现了两个索引表头,是否可以隐藏它们?非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
相关资源
最近更新 更多