如何从堆栈溢出复制/粘贴DataFrame到Python [英] How to copy/paste DataFrame from Stack Overflow into Python
问题描述
在问题和答案,用户经常发布示例DataFrame
,其问题/答案可用于该示例:
In questions and answers, users very often post an example DataFrame
which their question/answer works with:
In []: x
Out[]:
bar foo
0 4 1
1 5 2
2 6 3
能够将此DataFrame
放入我的Python解释器中非常有用,这样我就可以开始调试问题或测试答案了.
It'd be really useful to be able to get this DataFrame
into my Python interpreter so I can start debugging the question, or testing the answer.
我该怎么做?
推荐答案
熊猫是由真正知道人们想要做什么的人编写的.
Pandas is written by people that really know what people want to do.
从版本0.13
开始,有一个功能 pd.read_clipboard
在使此正常工作"方面荒唐有效.
Since version 0.13
there's a function pd.read_clipboard
which is absurdly effective at making this "just work".
将代码的一部分复制并粘贴到以bar foo
开头的问题(即DataFrame)中,并在Python解释器中执行以下操作:
Copy and paste the part of the code in the question that starts bar foo
, (i.e. the DataFrame) and do this in a Python interpreter:
In [53]: import pandas as pd
In [54]: df = pd.read_clipboard()
In [55]: df
Out[55]:
bar foo
0 4 1
1 5 2
2 6 3
注意事项
- 不包含iPython
In
或Out
东西,否则将无法正常工作 - 如果您有命名索引,则当前需要添加
engine='python'
(请参阅此问题(在GitHub上).命名索引时,"c"引擎当前已损坏. - MultiIndexes并不出色:
- Don't include the iPython
In
orOut
stuff or it won't work - If you have a named index, you currently need to add
engine='python'
(see this issue on GitHub). The 'c' engine is currently broken when the index is named. - It's not brilliant at MultiIndexes:
Caveats
尝试一下:
0 1 2
level1 level2
foo a 0.518444 0.239354 0.364764
b 0.377863 0.912586 0.760612
bar a 0.086825 0.118280 0.592211
这根本不起作用,或者这样:
which doesn't work at all, or this:
0 1 2
foo a 0.859630 0.399901 0.052504
b 0.231838 0.863228 0.017451
bar a 0.422231 0.307960 0.801993
可以使用,但返回的内容完全不正确!
Which works, but returns something totally incorrect!
这篇关于如何从堆栈溢出复制/粘贴DataFrame到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!