【问题标题】:Pandas DataFrame to Bootstrap TablePandas DataFrame 到引导表
【发布时间】:2018-01-23 21:33:30
【问题描述】:

我已通过将 DataFrame 表转换为 Json 对象成功地将其传递给 html。这是 Json 对象的样子。

accrued_income:{1501545600000: 236368.08, 1501632000000: 239957.09, Pct_Change: 1.5183987618}
nav:{1501545600000: 24.37265947, 1501632000000: 24.43271182, Pct_Change: 0.2463922744}
outstanding_shares_par:{1501545600000: 865725.234, 1501632000000: 865725.234, Pct_Change: 0}
security_value:{1501545600000: 19863626.03, 1501632000000: 19938086.06, Pct_Change: 0.3748561813}
shareholder_equity:{1501545600000: 21100026.32, 1501632000000: 21152015.16, Pct_Change: 0.2463922993}
total_assets:{1501545600000: 21198382.3, 1501632000000: 21250293, Pct_Change: 0.2448804785}

现在我想在Bootstrap Table. 上显示它,共有三列:15015456000001501632000000 和“Pct_Change”。前两列基本上是将 DateTime 转换为 unix 格式。此外,我希望 keys 成为行标签。基本上我想要一个看起来像这样的表:

【问题讨论】:

  • 如果我错了,请纠正我,我知道您已经使用数据框 to_json() 方法将 DataFrame 解析为 JSON? [pandas.pydata.org/pandas-docs/stable/generated/…。现在你正试图把它变成一个普通的表格(并用引导程序设置它的样式?)
  • 没错

标签: javascript python json


【解决方案1】:

除非您使用 JSON 执行无法使用 pandas 执行的操作,否则您可以直接将 DataFrame 从 pandas 解析为 HTML 表,并在执行此操作时分配适当的 CSS 类。

# Creates the dataframe
df = pd.DataFrame(myData)

# Parses the dataframe into an HTML element with 3 Bootstrap classes assigned.
html = df.to_html(classes=["table-bordered", "table-striped", "table-hover"])

有关to_html() 方法的更多信息:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html

【讨论】:

  • 好的,我将如何修改单个元素?
  • 修改单个元素是什么意思。你能举一个你想做的例子吗?
  • to_html() 是该过程的最后一步。如果您需要不同的字段/列名称或其他对数据进行分类和分组的方式,那么您应该在 pandas 的域中进行这些更改
  • 把类作为classes=["table table-bordered table-striped table-hover"]而不是逗号分隔。我遇到了 Bootstrap 3.4.1 的问题。
【解决方案2】:
#name of the table class

classes = 'table table-striped table-bordered table-hover table-sm'

#table type inside the classes

html_table = df.to_html(classes=classes)

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2013-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多