1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| user_data = { 'CustomerID': [1, 2, 3, 4, 5], 'Name': ['Rose', 'Bob', 'Jack', 'David', 'Lucy'], 'Email': ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'] } df_user = pd.DataFrame(user_data)
buy_data = { 'CustomerID': [1 ,2, 1, 3, 4, 3, 6], 'OrderID': [101, 102, 103, 104,105, 106, 107], 'Procuct': ['A', 'B', 'C', 'D', 'E', 'A', 'B'], 'Quantity': [2, 1, 3, 2, 4, 1, 2] } df_buy= pd.DataFrame(buy_data)
df_merge = pd.merge(df_user, df_buy, on='CustomerID', how='inner') print(df_merge) ''' CustomerID Name Email OrderID Procuct Quantity 0 1 Rose [email protected] 101 A 2 1 1 Rose [email protected] 103 C 3 2 2 Bob [email protected] 102 B 1 3 3 Jack [email protected] 104 D 2 4 3 Jack [email protected] 106 A 1 5 4 David [email protected] 105 E 4 '''
df_merge = pd.merge(df_user, df_buy, on='CustomerID', how='outer') print(df_merge) ''' CustomerID Name Email OrderID Procuct Quantity 0 1 Rose [email protected] 101.0 A 2.0 1 1 Rose [email protected] 103.0 C 3.0 2 2 Bob [email protected] 102.0 B 1.0 3 3 Jack [email protected] 104.0 D 2.0 4 3 Jack [email protected] 106.0 A 1.0 5 4 David [email protected] 105.0 E 4.0 6 5 Lucy [email protected] NaN NaN NaN 7 6 NaN NaN 107.0 B 2.0 '''
df_merge = pd.merge(df_user, df_buy, on='CustomerID', how='left') print(df_merge) ''' CustomerID Name Email OrderID Procuct Quantity 0 1 Rose [email protected] 101.0 A 2.0 1 1 Rose [email protected] 103.0 C 3.0 2 2 Bob [email protected] 102.0 B 1.0 3 3 Jack [email protected] 104.0 D 2.0 4 3 Jack [email protected] 106.0 A 1.0 5 4 David [email protected] 105.0 E 4.0 6 5 Lucy [email protected] NaN NaN NaN '''
df_merge = pd.merge(df_user, df_buy, on='CustomerID', how='right') print(df_merge) ''' CustomerID Name Email OrderID Procuct Quantity 0 1 Rose [email protected] 101 A 2 1 2 Bob [email protected] 102 B 1 2 1 Rose [email protected] 103 C 3 3 3 Jack [email protected] 104 D 2 4 4 David [email protected] 105 E 4 5 3 Jack [email protected] 106 A 1 6 6 NaN NaN 107 B 2 '''
|