1new_df = pd.merge(A_df, B_df, how='left', left_on=['A_c1','c2'], right_on = ['B_c1','c2'])
1def merge_sort(arr):
2 # The last array split
3 if len(arr) <= 1:
4 return arr
5 mid = len(arr) // 2
6 # Perform merge_sort recursively on both halves
7 left, right = merge_sort(arr[:mid]), merge_sort(arr[mid:])
8
9 # Merge each side together
10 return merge(left, right, arr.copy())
11
12
13def merge(left, right, merged):
14
15 left_cursor, right_cursor = 0, 0
16 while left_cursor < len(left) and right_cursor < len(right):
17
18 # Sort each one and place into the result
19 if left[left_cursor] <= right[right_cursor]:
20 merged[left_cursor+right_cursor]=left[left_cursor]
21 left_cursor += 1
22 else:
23 merged[left_cursor + right_cursor] = right[right_cursor]
24 right_cursor += 1
25
26 for left_cursor in range(left_cursor, len(left)):
27 merged[left_cursor + right_cursor] = left[left_cursor]
28
29 for right_cursor in range(right_cursor, len(right)):
30 merged[left_cursor + right_cursor] = right[right_cursor]
31
32 return merged
1# df1 as main df and use the feild from df2 and map it into df1
2
3df1.merge(df2,on='columnName',how='left')
1import pandas as pd
2df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'],
3 'value': [1, 2, 3, 5]})
4df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'],
5 'value': [5, 6, 7, 8]})
6df1.merge(df2, left_on='lkey', right_on='rkey')
1>>> df1.merge(df2, left_on='lkey', right_on='rkey')
2 lkey value_x rkey value_y
30 foo 1 foo 5
41 foo 1 foo 8
52 foo 5 foo 5
63 foo 5 foo 8
74 bar 2 bar 6
85 baz 3 baz 7
9
1def merge(array, left_index, right_index, middle):
2 # Make copies of both arrays we're trying to merge
3
4 # The second parameter is non-inclusive, so we have to increase by 1
5 left_copy = array[left_index:middle + 1]
6 right_copy = array[middle+1:right_index+1]
7
8 # Initial values for variables that we use to keep
9 # track of where we are in each array
10 left_copy_index = 0
11 right_copy_index = 0
12 sorted_index = left_index
13
14 # Go through both copies until we run out of elements in one
15 while left_copy_index < len(left_copy) and right_copy_index < len(right_copy):
16
17 # If our left_copy has the smaller element, put it in the sorted
18 # part and then move forward in left_copy (by increasing the pointer)
19 if left_copy[left_copy_index] <= right_copy[right_copy_index]:
20 array[sorted_index] = left_copy[left_copy_index]
21 left_copy_index = left_copy_index + 1
22 # Opposite from above
23 else:
24 array[sorted_index] = right_copy[right_copy_index]
25 right_copy_index = right_copy_index + 1
26
27 # Regardless of where we got our element from
28 # move forward in the sorted part
29 sorted_index = sorted_index + 1
30
31 # We ran out of elements either in left_copy or right_copy
32 # so we will go through the remaining elements and add them
33 while left_copy_index < len(left_copy):
34 array[sorted_index] = left_copy[left_copy_index]
35 left_copy_index = left_copy_index + 1
36 sorted_index = sorted_index + 1
37
38 while right_copy_index < len(right_copy):
39 array[sorted_index] = right_copy[right_copy_index]
40 right_copy_index = right_copy_index + 1
41 sorted_index = sorted_index + 1
42