Copying and Reading Pandas Dataframes to/from Clipboard
This note covers various operations involving copying and reading Pandas DataFrames to and from the system clipboard using pandas' built-in functionality.
The Palmer Penguins dataset is the starting point for the examples that follow. As shown in the video below and in this Jupyter Notebook, a new Dataframe has been created with the average body mass of each penguin species in the Palmer penguins dataset, calculated via an SQL-query and saved to the system clipboard.
import pandas as pd
from sqlite3 import connect
# memory SQLite database connection
conn = connect(':memory:')
url = 'https://tessarinseve.pythonanywhere.com/staticweb/palmerpenguins.csv'
df = pd.read_csv(url)
df.to_sql(name='palmerpenguins', con=conn)
avg = pd.read_sql('SELECT AVG(body_mass_g) as "Average Body Mass" ,species as Species FROM palmerpenguins GROUP BY species ', conn)
print(avg)
avg.to_clipboard()
The Dataframe can then be collected from the clipboard in an IPython console. Additionally, the DataFrame can be displayed as a bar chart using PandasGUI, which provides an interactive visualization of the data.
Bash Console
The same DataFrame can also be modified in a Bash console using an Awk script. On Windows OS it's possible to use the Powershell command Get-Clipboard to stream the contents of the clipboard on the standard output and the command clip.exe to redirect the pipe's output from the command line to the Windows clipboard.
powershell -command "Get-Clipboard" |awk -F'\t' 'FNR != 1 {print $2","$3}'|clip.exe
Alternatively, this small VLANG program:
// filename: readclipboad.v
import clipboard
fn main() {
mut c := clipboard.new()
println(c.get_text())
}
can also read from the system clipboard, and write to standard output. It can be compiled with:
v.exe readclipboad.v
which produces the executable readclipboad.exe.
Labplot
KDE LabPlot software is a scientific plotting and data analysis tool. The extended Python library for KDE LabPlot provides additional functionality to the software, including the ability to use Pandas Dataframes, and read and visualize data located in the system clipboard as shown at the end of the previous video.