測試環境為 CentOS 8 (虛擬機)
merge 函數就跟 SQL join 依樣可以將兩個資料表依據相對應的資料欄位合併起來.
安裝所需模組
[root@localhost ~]# pip install pandas
匯入模組
[root@localhost ~]# python3 Python 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pandas as pd
參考範例 – https://levelup.gitconnected.com/20-pandas-functions-for-80-of-your-data-science-tasks-b610c8bfe63c
>>> df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], ... 'value': [1, 2, 3, 4]}) >>> df1 key value 0 A 1 1 B 2 2 C 3 3 D 4
>>> df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], ... 'value': [5, 6, 7, 8]}) >>> df2 key value 0 B 5 1 D 6 2 E 7 3 F 8
依據 key 欄位來合併.
>>> merged_df = df1.merge(df2, on='key') >>> merged_df key value_x value_y 0 B 2 5 1 D 4 6
沒有解決問題,試試搜尋本站其他內容