Pandas 合併功能提供不同類型的連接:
left = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': np.random.randn(4)}) right = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': np.random.randn(4)}) left.merge(right, on='key') # Output: # key value_x value_y # 0 B 0.400157 1.867558 # 1 D 2.240893 -0.977278INNER JOIN
left.merge(right, on='key', how='left') # Output: # key value_x value_y # 0 A 1.764052 NaN # 1 B 0.400157 1.867558 # 2 C 0.978738 NaN # 3 D 2.240893 -0.977278LEFT OUTER IN
示例:
left.merge(right, on='key', how='right') # Output: # key value_x value_y # 0 B 0.400157 1.867558 # 1 D 2.240893 -0.977278 # 2 E NaN 0.950088 # 3 F NaN -0.151357
示例:
left.merge(right, on='key', how='outer') # Output: # key value_x value_y # 0 A 1.764052 NaN # 1 B 0.400157 1.867558 # 2 C 0.978738 NaN # 3 D 2.240893 -0.977278 # 4 E NaN 0.950088 # 5 F NaN -0.151357
以上是如何使用 Pandas Merge 執行不同類型的連結?的詳細內容。更多資訊請關注PHP中文網其他相關文章!