Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Smoking docs
https://pandas.pydata.org/docs/user_guide/merging.html
In [42]: left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
....: 'key2': ['K0', 'K1', 'K0', 'K1'],
....: 'A': ['A0', 'A1', 'A2', 'A3'],
....: 'B': ['B0', 'B1', 'B2', 'B3']})
....:
In [43]: right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
....: 'key2': ['K0', 'K0', 'K0', 'K0'],
....: 'C': ['C0', 'C1', 'C2', 'C3'],
....: 'D': ['D0', 'D1', 'D2', 'D3']})
....:
In [44]: result = pd.merge(left, right, how='left', on=['key1', 'key2'])
import pandas as pd
key = 'key'
left = pd.DataFrame({key: ['1', '2', '3', '4']})
right = pd.DataFrame({key: ['5', '4', '3', '2']})
df = pd.merge(left, right, on=key, how="outer", indicator=True)
print('Merged')
print(df)
df = df[df['_merge'] == 'left_only']
print('Result')
print(df)
Merged
key _merge
0 1 left_only
1 2 both
2 3 both
3 4 both
4 5 right_only
Result
key _merge
0 1 left_only
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question