DataFrame.
merge
Merge DataFrame objects with a database-style join.
0…n if no index is used for merging
Index of the left DataFrame if merged only on the index of the right DataFrame
Index of the right DataFrame if merged only on the index of the left DataFrame
e.g. if left with indices (a, x) and right with indices (b, x), the result will be an index (x, a, b)
{‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’
key order unlike pandas.
lexicographically.
not preserve the order of the left keys unlike pandas.
is None and not merging on indexes then this defaults to the intersection of the columns in both DataFrames.
be an array or list of arrays of the length of the left DataFrame. These arrays are treated as if they are columns.
be an array or list of arrays of the length of the right DataFrame. These arrays are treated as if they are columns.
MultiIndex, the number of keys in the other DataFrame (either the index or a number of columns) must match the number of levels.
left_index.
respectively.
A DataFrame of the two merged objects.
See also
DataFrame.join
Join columns of another DataFrame.
DataFrame.update
Modify in place using non-NA values from another DataFrame.
DataFrame.hint
Specifies some hint on the current DataFrame.
broadcast
Marks a DataFrame as small enough for use in broadcast joins.
Notes
instead of NaN.
Examples
>>> df1 = ps.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'], ... 'value': [1, 2, 3, 5]}, ... columns=['lkey', 'value']) >>> df2 = ps.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'], ... 'value': [5, 6, 7, 8]}, ... columns=['rkey', 'value']) >>> df1 lkey value 0 foo 1 1 bar 2 2 baz 3 3 foo 5 >>> df2 rkey value 0 foo 5 1 bar 6 2 baz 7 3 foo 8
Merge df1 and df2 on the lkey and rkey columns. The value columns have the default suffixes, _x and _y, appended.
>>> merged = df1.merge(df2, left_on='lkey', right_on='rkey') >>> merged.sort_values(by=['lkey', 'value_x', 'rkey', 'value_y']) lkey value_x rkey value_y ...bar 2 bar 6 ...baz 3 baz 7 ...foo 1 foo 5 ...foo 1 foo 8 ...foo 5 foo 5 ...foo 5 foo 8
>>> left_psdf = ps.DataFrame({'A': [1, 2]}) >>> right_psdf = ps.DataFrame({'B': ['x', 'y']}, index=[1, 2])
>>> left_psdf.merge(right_psdf, left_index=True, right_index=True).sort_index() A B 1 2 x
>>> left_psdf.merge(right_psdf, left_index=True, right_index=True, how='left').sort_index() A B 0 1 None 1 2 x
>>> left_psdf.merge(right_psdf, left_index=True, right_index=True, how='right').sort_index() A B 1 2.0 x 2 NaN y
>>> left_psdf.merge(right_psdf, left_index=True, right_index=True, how='outer').sort_index() A B 0 1.0 None 1 2.0 x 2 NaN y