【发布时间】:2020-09-07 10:06:32
【问题描述】:
我的问题有点类似于这个问题:How to merge pandas on string contains?,但我需要不同的输出,而且问题本身有点复杂。所以我有 2 个类似于下面的数据框:
df1 = pd.DataFrame({'ref_name':['city-louisville','city-louisville','city-louisville', 'town-lexington','town-lexington','town-lexington'], 'un_name1':['CPU1','CPU2','GPU1','CPU1','CPU2','GPU1'], 'value1':[10,15,28,12,14,14]})
df2 = pd.DataFrame({'ref_name':['louisville','louisville','lexington','lexington'], 'un_name2':['CPU','GPU','CPU','GPU'], 'value2':[25,28,26,14]})
我需要基于ref_name 和un_name 基于它们中的子字符串加入。它们不会总是像这样干净,但我认为它是一个不错的小例子。所以在这种情况下我想要的输出看起来像这样:
ref_name | un_name1 | un_name2 | value1 | value2
---------------------------------------------------------
louisville| CPU1 | CPU | 10 | 25
louisville| CPU2 | CPU | 15 | 25
louisville| GPU1 | GPU | 28 | 28
lexington | CPU1 | CPU | 12 | 26
lexington | CPU2 | CPU | 14 | 26
lexington | GPU1 | GPU | 14 | 14
提前感谢您对此的任何帮助!
【问题讨论】: