最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pandas DataFrame to Bootstrap Table - Stack Overflow

programmeradmin1浏览0评论

I have successfully passed a DataFrame table to html by converting it to a Json Object. Here is what the Json object looks like.

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}

Now I want to display this on a Bootstrap Table. There are three columns: 1501545600000, 1501632000000, and 'Pct_Change'. The first two columns basically are DateTime converted into a unix format. Furthermore, I want the the keys to be row labels. Essentially I want a table that looks like this:

I have successfully passed a DataFrame table to html by converting it to a Json Object. Here is what the Json object looks like.

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}

Now I want to display this on a Bootstrap Table. There are three columns: 1501545600000, 1501632000000, and 'Pct_Change'. The first two columns basically are DateTime converted into a unix format. Furthermore, I want the the keys to be row labels. Essentially I want a table that looks like this:

Share Improve this question edited Aug 16, 2017 at 12:26 Tony Babarino 3,4054 gold badges33 silver badges45 bronze badges asked Aug 15, 2017 at 21:19 anderishanderish 1,7596 gold badges27 silver badges61 bronze badges 2
  • Correct me if I'm wrong, I understand you've parsed the DataFrame to JSON with the dataframe to_json() method? [pandas.pydata.org/pandas-docs/stable/generated/…. And now you're attempting to turn that into a regular table (and style it with bootstrap?) – mmenschig Commented Aug 15, 2017 at 21:29
  • That is correct – anderish Commented Aug 15, 2017 at 21:30
Add a comment  | 

2 Answers 2

Reset to default 21

Unless you're doing something with JSON that you cannot perform with pandas, you can directly parse your DataFrame from pandas into an HTML table and assign the appropriate CSS classes while doing it.

# 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"])

More info about the to_html() method: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html

#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)
发布评论

评论列表(0)

  1. 暂无评论