我有一個dataframe1包含500,000行。我想通過查找包含配置的dataframe2中的型號來填充配置列。如何根據groupby函數輸出向熊貓數據框添加新列?
Dataframe1:
Model Date Status Configuration
A4 10/2014 Inop
A4 11/2014 Op
A4 11/2014 Op
G5 10/2014 Inop
G5 11/2014 Inop
G5 11/2014 Op
G8 10/2014 Op
G8 11/2014 Op
G8 11/2014 Op
G8 10/2014 Inop
Z2 11/2014 Op
Z2 11/2014 Op
Dataframe2:
Model Configuration
A4 ICS
G5 PCS
G8 ICS
Z2 1/2 ICS
代碼我目前正在運行:
for Model, group in dataframe1.groupby('Model'):
#gets configuration from dataframe2
config = get_configuration(Model)
#attempt to assign configuration to all columns with that model number in dataframe1
dataframe1['Config'] = con
此代碼返回:
此代碼組dataframe1通過模型併成功地獲得各組的配置,但我不能說配置應用到一個新的行dataframe1了以下結果:
Model Date Status Configuration
A4 10/2014 Inop ICS
A4 11/2014 Op ICS
A4 11/2014 Op ICS
G5 10/2014 Inop PCS
G5 11/2014 Inop PCS
G5 11/2014 Op PCS
G8 10/2014 Op ICS
G8 11/2014 Op ICS
G8 11/2014 Op ICS
G8 10/2014 Inop ICS
Z2 11/2014 Op 1/2 ICS
Z2 11/2014 Op 1/2 ICS
試試這個鏈接https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html – Wen