@@ -89,8 +89,9 @@ Joining on a key
89
89
~~~~~~~~~~~~~~~~
90
90
91
91
``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:
94
95
95
96
.. ipython :: python
96
97
@@ -101,6 +102,44 @@ best illustrated by example:
101
102
to_join
102
103
df.join(to_join, on = ' key' )
103
104
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
+
104
143
Merging ordered records
105
144
~~~~~~~~~~~~~~~~~~~~~~~
106
145
0 commit comments