简体   繁体   English

如何合并或连接两个数据框,但保留两者的某些列

[英]How merge or join two dataframes, but keeping certain columns of both

How merge or join two dataframes, but keeping certain columns of both?如何合并或连接两个数据框,但保留两者的某些列?

I need to merge this two dataframes into one.我需要将这两个数据框合并为一个。 dataframe 2 has all the columns dataframe 1 just need the column "leads" dataframe 2 有所有列 dataframe 1 只需要列“leads”

Dataframe1数据框1

campaignid leads 
35119190 391 
31664745 365 
4899110 211 
325772660 195 
64002140 131 
143679198 58 
283494007 45 

Dataframe2数据框2

campaignid cost time reach 
35119190 391 391 391 
31664745 365 391 391 
4899110 211 391 391 
325772660 195 391 391 
64002140 131 391 391 
143679198 58 391 391 
283494007 45 391 391

Desired result:期望的结果:

Dataframe2数据框2

campaignid cost time reach leads 
35119190 391 391 391 391 
31664745 365 391 391 365 
4899110 211 391 391 211 
325772660 195 391 391 195 
64002140 131 391 391 131 
143679198 58 391 391 58 
283494007 45 391 391 45 

g_spend.to_dict() g_spend.to_dict()

{'id': {0: 35119190,
  1: 64002140,
  2: 272351300,
  3: 4899110,},
 'Campaign_ID_name': {0: 'brand',
  1: '-',
  2: '-',
  3: 'science',
,
 'Month': {0: '2019|08',
  1: '2019|08',
  2: '2019|08',
  3: '2019|08',
},
 'Account': {0: 'a',
  1: 'a',
  2: 'b',
  3: 'c',
},
 'campaignid': {0: 35119190,
  1: 64002140,
  2: 272351300,
  3: 4899110,
 },
 'campaign_name': {0: 'All_Brand',
  1: 'All',
  2: 'All_GBHS',
  3: 'All_Science',
},
 'cost': {0: '$59,399.37 ',
  1: '$12,660.37 ',
  2: '$5,631.96 ',
}}

grouped_cw.to_dict() grouped_cw.to_dict()

{'leads': {'1076533154': 40.0,
  '143679198': 58.0,
  '169278078': 13.0,
  '1729099155': 8.0,
}}
pd.merge(Dataframe1, Dataframe2, on='campaignid')

Let's use map :让我们使用map

df2['leads'] = df2['campaignid'].map(df1.set_index('campaignid')['leads'])
df2

Output: Output:

   campaignid  cost  time  reach  leads
0    35119190   391   391    391    391
1    31664745   365   391    391    365
2     4899110   211   391    391    211
3   325772660   195   391    391    195
4    64002140   131   391    391    131
5   143679198    58   391    391     58
6   283494007    45   391    391     45

Try尝试

df2['leads'] = df2['campaignid'].map(grouped_cw)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在索引和列上合并两个数据框 - how to merge two dataframes on both indexes and columns 在某些列上合并两个DataFrame - Merge two DataFrames on certain columns 合并多个列上的两个数据框,但仅在两个列都不是 NaN 时才合并列 - Merge two dataframes on multiple columns but only merge on columns if both not NaN 通过索引和列合并使用MultiIndex列合并/连接/附加两个Pandas DataFrame - Merge/Join/Append two Pandas DataFrames with MultiIndex columns by both index and cols 如何在将两个 Pandas 数据帧的列保留在新数据帧中的同时组合两个 Pandas 数据帧? - How can i combine two Pandas dataframes while keeping columns of both in the new dataframe? 如何通过合并公共列并保留 rest 来合并两个大型 pandas 数据帧,无需键 - How to merge two large pandas dataframes by mergining common columns and keeping the rest, without keys 如何加入/合并 2 个 DataFrame,同时保留第二个 DataFrame 的值? - How can I join/merge 2 DataFrames, keeping values from the second? 如何基于在某些行中可能以相反顺序排列的两个不同列合并两个数据框? - How to merge two dataframes based on two different columns that could be in reverse order in certain rows? 在两列上合并DataFrame - Merge DataFrames on two columns 如何一次合并两列数据框 - How to merge dataframes by two columns at once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM