3 回答
TA贡献1744条经验 获得超4个赞
这是我将如何解决这个问题:
# First we make two dataframes out of the dictionaries with pd.melt
df2 = pd.DataFrame(prod_dict, index=[0])
df3 = pd.DataFrame(pack_dict, index=[0])
df2 = df2.melt(var_name=['PROD_TYPE'], value_name = 'ITEM')
df3 = df3.melt(var_name=['PKG_TYPE'], value_name = 'ITEM')
# df2
PROD_TYPE ITEM
0 PK Packaging
1 ML Mix
2 CM Textile
3 NK Metallic
# df3
PKG_TYPE ITEM
0 PK3 Misc Packaging
1 PK4 Mix Packaging
2 PK9 Textile Packaging
# Now we can merge our information together on keycolumns PROD_TYPE and PKG_TYPE
df_final = pd.merge(df, df2, on='PROD_TYPE')
df_final = pd.merge(df_final, df3, on='PKG_TYPE')
PROD_TYPE PKG_TYPE VALUE ITEM_x ITEM_y
0 PK PK3 1000 Packaging Misc Packaging
1 ML PK4 900 Mix Mix Packaging
2 ML PK4 800 Mix Mix Packaging
3 CM PK9 700 Textile Textile Packaging
# Finally we use np.where to conditionally select the values we need
df_final['ITEM'] = np.where(df_final.PROD_TYPE == 'PK', df_final.ITEM_y, df_final.ITEM_x)
# Drop columns which are not needed in output
df_final.drop(['ITEM_x', 'ITEM_y'], axis=1, inplace=True)
输出
PROD_TYPE PKG_TYPE VALUE ITEM
0 PK PK3 1000 Misc Packaging
1 ML PK4 900 Mix
2 ML PK4 800 Mix
3 CM PK9 700 Textile
np.where来自numpy模块,工作原理如下:
np.where(condition, true value, false value)
TA贡献1829条经验 获得超13个赞
类似于@Erfan 的回答,使用numpy.where但跳过meltto use pd.Series.map()。使用问题中的变量:
In []: df['ITEM'] = pd.np.where(df.PROD_TYPE == "PK",
df.PKG_TYPE.map(pack_dict),
df.PROD_TYPE.map(prod_dict))
In []: df
Out[]:
PROD_TYPE PKG_TYPE VALUE ITEM
0 PK PK3 1000 Misc Packaging
1 ML PK4 900 Mix
2 ML PK4 800 Mix
3 CM PK9 700 Textile
请注意,numpy已经由 加载pandas,只需使用pd.np.
TA贡献1829条经验 获得超7个赞
一种方法是:
df["ITEM"]= [pack_dict[row[1]["PKG_TYPE"]]
if row[1]["PROD_TYPE"] == "PK"
else prod_dict[row[1]["PROD_TYPE"]]
for row in df.iterrows()]
我发现这比 Erfan 的解决方案快 10 倍。
添加回答
举报