Pandas How To Uncategorized How to export data from Pandas to HTML

How to export data from Pandas to HTML

Pandas makes it easy to export data to HTML format using the to_html() method. Here’s an example of how to do it:

import pandas as pd

# create a sample dataframe
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)

# convert the dataframe to HTML
html = df.to_html()

# write the HTML to a file
with open('output.html', 'w') as f:
f.write(html)

In this example, we first create a sample dataframe using the pd.DataFrame() function. We then use the to_html() method to convert the dataframe to HTML format and store the result in a variable called html. Finally, we write the HTML to a file called output.html using the open() function and the ‘w’ mode.

By default, to_html() generates a table with the dataframe’s data. You can also customize the appearance of the table by passing various parameters to the method. For example, you can add CSS classes to the table, set the table’s caption, and more. Check out the pandas documentation for more details on how to customize the output of to_html().

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post