Julia: How do I copy a DataFrame to the system clipboard?

Viewed 1003

In Julia, I would like to be able to copy a DataFrame (from the DataFrames.jl package) to the system clipboard in a format that allows me to easily paste it into another program like Excel.

If I just try clipboard(df) then it just gives me the output as if I ran print(df) at the console, which doesn't paste cleanly into Excel.

1 Answers

Use the sprint() and show() functions in combination around the DataFrame first, as below:

using DataFrames
df = DataFrame(rand(2, 3));
clipboard(sprint(show, "text/tab-separated-values", df))

Since you're asking about Excel in particular, the "text/tab-separated-values" MIME type will structure the output such that it will paste cleanly into Excel cells. You could also specify other types, such as comma-separated:

clipboard(sprint(show, "text/csv", df))

Check out the documentation on sprint() and show() for more details on how this works, it's quite flexible for other use cases as well.

Related