我想要做的就是創建一個包含在表中的所有數據的新表1與另外一個名爲列說明(值從表2)應該匹配部件號在表1。
我試過使用df.merge
但是,它只是使Table 3
超過三行。
我用於生產上述圖像的代碼,
pd.merge(xl_csv, xl_df, on="Part Number", how="left")[['Part Number', 'Occurrence Count', 'G1 TAT_x', 'Description']]
我想要做的就是創建一個包含在表中的所有數據的新表1與另外一個名爲列說明(值從表2)應該匹配部件號在表1。
我試過使用df.merge
但是,它只是使Table 3
超過三行。
我用於生產上述圖像的代碼,
pd.merge(xl_csv, xl_df, on="Part Number", how="left")[['Part Number', 'Occurrence Count', 'G1 TAT_x', 'Description']]
看來需要LEFT JOIN,然後通過子集選擇列,但首先需要通過drop_duplicates
在df2
刪除重複:
cols = ['Part Num','Sample','Description']
df = pd.merge(df1, df2.drop_duplicates('Part Num'), on='Part Num', how='left')[cols]
print (df)
Part Num Sample Description
0 1 one Desc. one
1 2 two Desc. two
2 3 three Desc. three
map
的另一個解決方案:
df1['Description'] = df1['Part Num'].map(df2.drop_duplicates('Part Num')
.set_index('Part Num')['Description'])
print (df1)
Part Num Sample Description
0 1 one Desc. one
1 2 two Desc. two
2 3 three Desc. three
表2實際上有兩個以上的列,但我只是想** **說明**。 –
查看我的更新,'merge'將**零件編號**展開成多行。 –
是的,你有重複的問題。所以需要冷杉刪除它們。 – jezrael
我不知道指數是什麼。 – piRSquared