In ipython
I can run something such as
import pandas as pd
pd.DataFrame.join?
and view help for DataFrame
.
At the end of the help file there are often examples, such as
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
I'm wondering whether there's a quick approach to extracting these examples into the current session so that I may look into them further, or if i have to copy paste (then adjust) the code from the help file.
For generic Python examples, copy the example then just paste it; IPython is smart enough to understand Python's >>>
and ...
prefixes, as well as it's own prefix format. See Pasting of code starting with Python or IPython prompts in the IPython manual.
I'm copying the first two examples from the documentation here:
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']})
You can also use the %paste
terminal magic command to have IPython do the same; this command pulls the data straight from your clipboard.
Here I put the 3rd example onto the clipboard:
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
There is also the %cpaste
magic command, which presents a prompt for you to paste Python code into. You can do so multiple times (from separate sections, perhaps), until you enter a --
double dash on a line by itself. This does require you have your examples lined up in another location to copy from, or that you use a clipboard that lets you recall previous entries.
You can also copy just the df
dataframe output and have Pandas read it from your clipboard, using the pandas.read_clipboard()
function; start copying from the columns line onwards; this is the final dataframe output shown in the documentation:
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
I used \s\s+
rather than the default \s+
; it's more robust as it'll let you accept column names with a single space in them.