-
-
Save kkashyap1707/ee94d15cc0b4724b14caff7a969a6b46 to your computer and use it in GitHub Desktop.
Pandas dataframe with bootstrap styling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[1]: | |
data = [ | |
{"name": "Argentina", "year": 2005, "continent": "South America", "form": "Republic", "gdp": 181.357, "oil": 1.545, "balance": 4.699}, | |
{"name": "Argentina", "year": 2006, "continent": "South America", "form": "Republic", "gdp": 212.507, "oil": 1.732, "balance": 7.167}, | |
{"name": "Australia", "year": 2012, "continent": "Australasia", "form": "Constitutional monarchy", "gdp": 1542.055, "oil": 22.596, "balance": -62.969}, | |
{"name": "Australia", "year": 2013, "continent": "Australasia", "form": "Constitutional monarchy", "gdp": 1598.069, "oil": 23.650, "balance": -87.944}, | |
{"name": "Austria", "year": 2005, "continent": "Europe", "form": "Republic", "gdp": 305.513, "oil": 7.600, "balance": 6.615}, | |
{"name": "Austria", "year": 2006, "continent": "Europe", "form": "Republic", "gdp": 325.256, "oil": 9.344, "balance": 9.112}, | |
{"name": "Austria", "year": 2009, "continent": "Europe", "form": "Republic", "gdp": 384.622, "oil": 8.475, "balance": 10.428}, | |
{"name": "Belarus", "year": 2009, "continent": "Europe", "form": "Republic", "gdp": 49.209, "oil": 8.386, "balance": -6.178}, | |
{"name": "Belarus", "year": 2010, "continent": "Europe", "form": "Republic", "gdp": 55.221, "oil": 7.686, "balance": -8.278}, | |
{"name": "Belarus", "year": 2013, "continent": "Europe", "form": "Republic", "gdp": 58.933, "oil": 11.434, "balance": -3.419}, | |
{"name": "Belgium", "year": 2005, "continent": "Europe", "form": "Constitutional monarchy", "gdp": 378.006, "oil": 24.956, "balance": 7.465}, | |
{"name": "Brazil", "year": 2011, "continent": "South America", "form": "Republic", "gdp": 2492.907, "oil": 37.498, "balance": -52.480}, | |
{"name": "Brazil", "year": 2012, "continent": "South America", "form": "Republic", "gdp": 2425.052, "oil": 37.763, "balance": -62.341}, | |
] | |
# In[2]: | |
import pandas as pd | |
df = pd.DataFrame(data) | |
# In[10]: | |
template = """" | |
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> | |
<style> | |
th { | |
background: #2d2d71; | |
color: white; | |
text-align: left; | |
} | |
</style> | |
<body> | |
%s | |
</body> | |
""" | |
# check https://getbootstrap.com/docs/4.0/content/tables/ | |
classes = 'table table-striped table-bordered table-hover table-sm' | |
html= template % df.to_html(classes=classes) | |
file_name = 'test.html' | |
with open(file_name,'w') as f: | |
f.write(html) | |
# In[ ]: | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
╰─python3 pandas.py 1 ↵
Traceback (most recent call last):
File "pandas.py", line 6, in
import pandas as pd
File "/Users/keshavkashyap/PycharmProjects/htmltopdfpython/dataFrameStyling/pandas.py", line 26, in
df = pd.DataFrame(data)
AttributeError: module 'pandas' has no attribute 'DataFrame'