I have 2 dataframes, one has a key column which have duplicates. They other key doesn`t have duplicates. I'd like to merge these dataframes on that key, it's like many correspondence key if df1 to one key in df2. For example:
df1 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K2', 'K2', 'K3'],
'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']},
columns=['key', 'A'])
df2 = pd.DataFrame({'B': ['B0', 'B1', 'B2', 'B3', 'B4'],
'key': ['K0', 'K1', 'K2', 'K3', 'K4']},
columns=['key', 'B'])
key A
0 K0 A0
1 K1 A1
2 K2 A2
3 K2 A3
4 K2 A4
5 K3 A5
key B
0 K0 B0
1 K1 B1
2 K2 B2
3 K3 B3
I'm trying to get the following output
key A B
0 K0 A0 B0
1 K1 A1 B1
2 K2 A2 B2
3 K2 A3 B2
4 K2 A4 B2
5 K3 A5 B3
Two tables have much more columns than the example. how I can accomplish this? many thanks~