Writing data from Pandas DataframesΒΆ

Once data is loaded in a Dataframe object in Pandas, you have the ability to transform that data to various different formats including the clipboard. Most destinations will require a path on the filesystem for writing the output.

Start with a DataframeΒΆ

Remember, you have to have a Dataframe already to export data. The pd class will not allow you to export from it.

import pandas as pd
df = pd.read_csv("world-championship-qualifier.csv")
df

Write to many other destinationsΒΆ

The destinations are from a Dataframe object itself, not from the pd object.

Including the clipboard!

  • to_clipboard

  • to_csv

  • to_dict

  • to_excel

  • to_feather

  • to_gbq

  • to_hdf

  • to_html

  • to_json

  • to_latex

  • to_markdown

  • to_numpy

  • to_parquet

  • to_period

  • to_pickle

  • to_records

  • to_sql

  • to_stata

  • to_string

  • to_timestamp

  • to_xarray

  • to_xml

# export a dataset to HTML
df.to_html("dataset.html")

Copy/Paste into other formatsΒΆ

This flexibility of Pandas to allow you to read and write to many different formats and destinations can be used to create quick utilities like transforming a CSV file to paste to Excel or Markdown

!pip install tabulate
# to markdown and then to clipboard
# this needs tabulate dependency
from pandas.io.clipboards import to_clipboard
md = df.to_markdown()
to_clipboard(md, excel=False)