Could there be an easier way to use pandas read_clipboard to read a Series?

北战南征 提交于 2021-02-07 10:01:10

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!