Blue Flower

IPython notebook is great for prototyping, making plots, interactive work, but loading data from file might feel tedious when all you want is to quickly test an idea or generate a plot. It would be nice to copy the data to clipboard from Excel or another spreadsheet application and access it from Python script. Sure you can paste it right into notebook as a multi line string in triple quotes and then split and convert into numbers. But it clutters your code. Clipboard access is usually available from GUI tool kits you don't want to use and might be system dependent, making your code not portable.


Fortunately Pandas library also offer routines to read and write clipboard. Pandas is supplied with Scientific Python distributions so if you installed one of those you don't need to worry about dependencies. I've already shown how to use Pandas to Convert Excel file into SQLite database. Now let's use it to get data from clipboard.  In two lines of code Pandas will make the data copied from Excel available to Python script as a numpy array.

import pandas as pd

The following code will print the imported array and plot the columns on a line plot

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline


The imported array is always 2d. If you import only one column of data, you might want to convert it in 1D array using the flatten() method


If you copied column header from Excel - don't use header=None

Leave comment
Please register to add comment