2

Some times, i want use read_clipboard to read Serieses, and i would have to do:

pd.Series(pd.read_clipboard(header=None).values[:,0])

So would it be nice if there was an easier way?

I can do it very easily for data-frames, like:

pd.read_clipboard()

And that's it.

But for Series, it's much longer-one-liner.

So is there an easier way?

That i don't know?

Any secretive code?

cs95
  • 379,657
  • 97
  • 704
  • 746
U13-Forward
  • 69,221
  • 14
  • 89
  • 114

2 Answers2

5

Copy this to clipboard:

1
2
3

Better would be to use squeeze=True as an argument.

pd.read_clipboard(header=None, squeeze=True)

0    1
1    2
2    3
Name: 0, dtype: int64

Which returns a Series. If you want to name the series, use the names parameter:

pd.read_clipboard(header=None, squeeze=True, names=['mycol'])

0    1
1    2
2    3
Name: mycol, dtype: int64

Actually, read_clipboard uses pyperclip to read from the clipboard, and sends the text to read_table.

Read up on the supported arguments.

cs95
  • 379,657
  • 97
  • 704
  • 746
2

I think simplest is remove Series constructor (read_clipboard return here one column DataFrame) and because header is None always column is 0:

s = pd.read_clipboard(header=None)[0]

Another solution with DataFrame.squeeze for pandas 0.20.0+ for converting one column DataFrame to Series:

s = pd.read_clipboard(header=None).squeeze()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252