Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying excel data into a python list in IPython using clipboard?

Tags:

python

ipython

Is there a way to perform the following workflow:

  1. Select cells in an Excel spreadsheet
  2. Copy them using Ctrl+C
  3. Get the content the selected cells in a form of a python list or numpy array into the IPython shell?
like image 765
Norfeldt Avatar asked Aug 19 '13 10:08

Norfeldt


1 Answers

Update: It seems that the readline thing that @PauloAlmeida mentioned is turned on by default in the 1.0 verison of IPython. So all you have to do is:

  1. from numpy import array
  2. Copy the cells from the spreadsheet
  3. Hit Alt+V instead of Ctrl+V

And you will get in IPython something like:

array([[1, 1], [2, 2]])

Or you can use the pandas library read_clipboard method.

import pandas as pd
pd.read_clipboard()            # If you have selected the headers
pd.read_clipboard(header=None) # If you haven't selected the headers

This will return you a pandas DataFrame object which acts similarly to a spreadsheet. You can find more about it in their official documentation.

like image 170
Viktor Kerkez Avatar answered Sep 29 '22 06:09

Viktor Kerkez