Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 44a15a6

Browse files
committedOct 23, 2011
DOC: more docs holes on joining, etc.
1 parent 8ec1c97 commit 44a15a6

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed
 

‎TODO.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ TODO docs
3535
- DONE Index / MultiIndex names
3636
- DONE Unstack / stack by level name
3737
- DONE name attribute on Series
38+
- DONE Multi-key joining
39+
- DONE Inner join on key
3840

39-
- Inner join on key
40-
- Multi-key joining
4141
- align functions
4242
- df[col_list]
4343
- Panel.rename_axis

‎doc/source/merging.rst

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ Joining on a key
8989
~~~~~~~~~~~~~~~~
9090

9191
``join`` takes an optional ``on`` argument which should be a column name in the
92-
calling DataFrame which will be used to "align" the passed DataFrame. This is
93-
best illustrated by example:
92+
calling DataFrame which will be used to "align" the passed DataFrame. The
93+
joining currently aligns the calling DataFrame's column (or columns) on the
94+
passed DataFrame's index. This is best illustrated by example:
9495

9596
.. ipython:: python
9697
@@ -101,6 +102,44 @@ best illustrated by example:
101102
to_join
102103
df.join(to_join, on='key')
103104
105+
To join on multiple keys, the passed DataFrame must have a ``MultiIndex``:
106+
107+
.. ipython:: python
108+
109+
index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
110+
['one', 'two', 'three']],
111+
labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
112+
[0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
113+
names=['first', 'second'])
114+
to_join = DataFrame(np.random.randn(10, 3), index=index,
115+
columns=['j_one', 'j_two', 'j_three'])
116+
117+
# a little relevant example with NAs
118+
key1 = ['bar', 'bar', 'bar', 'foo', 'foo', 'baz', 'baz', 'qux',
119+
'qux', 'snap']
120+
key2 = ['two', 'one', 'three', 'one', 'two', 'one', 'two', 'two',
121+
'three', 'one']
122+
123+
data = np.random.randn(len(key1))
124+
data = DataFrame({'key1' : key1, 'key2' : key2,
125+
'data' : data})
126+
data
127+
to_join
128+
129+
130+
.. ipython:: python
131+
132+
data.join(to_join, on=['key1', 'key2'])
133+
134+
This is by default a "many-to-one" or "VLOOKUP"-style left join operation. An
135+
inner join is also supported:
136+
137+
.. ipython:: python
138+
139+
data.join(to_join, on=['key1', 'key2'], how='inner')
140+
141+
This drops any rows where there was no match.
142+
104143
Merging ordered records
105144
~~~~~~~~~~~~~~~~~~~~~~~
106145

0 commit comments

Comments
 (0)
Please sign in to comment.