Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将python帮助中的示例提取到ipython会话中_Python_Python 3.x_Pandas_Ipython_Manpage - Fatal编程技术网

将python帮助中的示例提取到ipython会话中

将python帮助中的示例提取到ipython会话中,python,python-3.x,pandas,ipython,manpage,Python,Python 3.x,Pandas,Ipython,Manpage,在ipython中,我可以运行诸如 import pandas as pd pd.DataFrame.join? 并查看数据帧的帮助 在帮助文件的末尾通常有一些示例,例如 Another option to join using the key columns is to use the `on` parameter. DataFrame.join always uses `other`'s index but we can use any column in `df`. This metho

ipython
中,我可以运行诸如

import pandas as pd
pd.DataFrame.join?
并查看数据帧的帮助

在帮助文件的末尾通常有一些示例,例如

Another option to join using the key columns is to use the `on`
parameter. DataFrame.join always uses `other`'s index but we can use
any column in `df`. This method preserves the original DataFrame's
index in the result.

>>> df.join(other.set_index('key'), on='key')
  key   A    B
0  K0  A0   B0
1  K1  A1   B1
2  K2  A2   B2
3  K3  A3  NaN
4  K4  A4  NaN
5  K5  A5  NaN

我想知道是否有一种快速的方法可以将这些示例提取到当前会话中,以便我可以进一步研究它们,或者是否必须从帮助文件中复制粘贴(然后调整)代码

对于一般Python示例,复制示例,然后粘贴它;IPython足够聪明,能够理解Python的
>
..
前缀,以及它自己的前缀格式。请参阅IPython手册中的

我在这里复制文档中的前两个示例:

In [1]: import pandas as pd

In [2]: pd.DataFrame.join?

In [3]: >>> df = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
   ...: ...                    'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})

In [4]: df
Out[4]:
  key   A
0  K0  A0
1  K1  A1
2  K2  A2
3  K3  A3
4  K4  A4
5  K5  A5

In [5]: >>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
    ...: ...                       'B': ['B0', 'B1', 'B2']})
您也可以使用来让IPython执行相同的操作;此命令直接从剪贴板中提取数据

在这里,我将第三个示例放在剪贴板上:

In [6]: %paste
>>> df.set_index('key').join(other.set_index('key'))
## -- End pasted text --
Out[6]:
      A    B
key
K0   A0   B0
K1   A1   B1
K2   A2   B2
K3   A3  NaN
K4   A4  NaN
K5   A5  NaN
还有,它提示您将Python代码粘贴到中。您可以多次这样做(可能是从单独的部分开始),直到您在一行上单独输入一个
--
双破折号。这确实需要将示例排列在另一个位置进行复制,或者使用剪贴板来回忆以前的条目

您还可以仅复制
df
数据帧输出,并让Panda使用从剪贴板读取数据帧;从列行开始复制;这是文档中显示的最终数据帧输出:

In [7]: pd.DataFrame.join?

In [8]: pd.read_clipboard('\s\s+')
Out[8]:
  key   A    B
0  K0  A0   B0
1  K1  A1   B1
2  K2  A2   B2
3  K3  A3  NaN
4  K4  A4  NaN
5  K5  A5  NaN

我使用了
\s\s+
而不是默认的
\s+
;它更强大,因为它允许您接受包含单个空格的列名。

\s\s+
用于
sep
参数吗?
\s{2,}
也不起作用吗?@cs95:是的,这是
sep
参数,它是调用中的第一个位置参数<代码>\s{2,}也可以,是的。