|
| 1 | +# View more python tutorials on my Youtube and Youku channel!!! |
| 2 | + |
| 3 | +# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg |
| 4 | +# Youku video tutorial: http://i.youku.com/pythontutorial |
| 5 | + |
| 6 | +""" |
| 7 | +Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly. |
| 8 | +""" |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +# merging two df by key/keys. (may be used in database) |
| 12 | +# simple example |
| 13 | +left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'], |
| 14 | + 'A': ['A0', 'A1', 'A2', 'A3'], |
| 15 | + 'B': ['B0', 'B1', 'B2', 'B3']}) |
| 16 | +right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'], |
| 17 | + 'C': ['C0', 'C1', 'C2', 'C3'], |
| 18 | + 'D': ['D0', 'D1', 'D2', 'D3']}) |
| 19 | +print(left) |
| 20 | +print(right) |
| 21 | +res = pd.merge(left, right, on='key') |
| 22 | +print(res) |
| 23 | + |
| 24 | +# consider two keys |
| 25 | +left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'], |
| 26 | + 'key2': ['K0', 'K1', 'K0', 'K1'], |
| 27 | + 'A': ['A0', 'A1', 'A2', 'A3'], |
| 28 | + 'B': ['B0', 'B1', 'B2', 'B3']}) |
| 29 | +right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'], |
| 30 | + 'key2': ['K0', 'K0', 'K0', 'K0'], |
| 31 | + 'C': ['C0', 'C1', 'C2', 'C3'], |
| 32 | + 'D': ['D0', 'D1', 'D2', 'D3']}) |
| 33 | +print(left) |
| 34 | +print(right) |
| 35 | +res = pd.merge(left, right, on=['key1', 'key2'], how='inner') # default for how='inner' |
| 36 | +# how = ['left', 'right', 'outer', 'inner'] |
| 37 | +res = pd.merge(left, right, on=['key1', 'key2'], how='left') |
| 38 | +print(res) |
| 39 | + |
| 40 | +# indicator |
| 41 | +df1 = pd.DataFrame({'col1':[0,1], 'col_left':['a','b']}) |
| 42 | +df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]}) |
| 43 | +print(df1) |
| 44 | +print(df2) |
| 45 | +res = pd.merge(df1, df2, on='col1', how='outer', indicator=True) |
| 46 | +# give the indicator a custom name |
| 47 | +res = pd.merge(df1, df2, on='col1', how='outer', indicator='indicator_column') |
| 48 | + |
| 49 | + |
| 50 | +# merged by index |
| 51 | +left = pd.DataFrame({'A': ['A0', 'A1', 'A2'], |
| 52 | + 'B': ['B0', 'B1', 'B2']}, |
| 53 | + index=['K0', 'K1', 'K2']) |
| 54 | +right = pd.DataFrame({'C': ['C0', 'C2', 'C3'], |
| 55 | + 'D': ['D0', 'D2', 'D3']}, |
| 56 | + index=['K0', 'K2', 'K3']) |
| 57 | +print(left) |
| 58 | +print(right) |
| 59 | +# left_index and right_index |
| 60 | +res = pd.merge(left, right, left_index=True, right_index=True, how='outer') |
| 61 | +res = pd.merge(left, right, left_index=True, right_index=True, how='inner') |
| 62 | + |
| 63 | +# handle overlapping |
| 64 | +boys = pd.DataFrame({'k': ['K0', 'K1', 'K2'], 'age': [1, 2, 3]}) |
| 65 | +girls = pd.DataFrame({'k': ['K0', 'K0', 'K3'], 'age': [4, 5, 6]}) |
| 66 | +res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='inner') |
| 67 | +print(res) |
| 68 | + |
| 69 | +# join function in pandas is similar with merge. If know merge, you will understand join |
0 commit comments