问题
Some times, i want use read_clipboard
to read Series
es, 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?
回答1:
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.
回答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()
来源:https://stackoverflow.com/questions/54021079/could-there-be-an-easier-way-to-use-pandas-read-clipboard-to-read-a-series