2021年5月7日星期五

Merging dataframes and keeping columns in place?

I have the following data frames:

DF1

df1 = pd.DataFrame(columns = ['Key', 'Value'])  df1['Key'] = ['A', 'B', 'C', 'D']  

DF2

df2 = pd.DataFrame(columns = ['Key', 'Value'])  df2['Key'] = ['A', 'C']  df2['Value'] = [1,7]  

I would like to merge these two data frames such that the data from DF2 under the column 'Value' is filled in DF1, where the remaining letters 'B' and 'D' have zero.

I tried this:

df3 = pd.merge(df1,df2,how='outer', on = 'Key')  

However, this creates an additional column Value_x and Value_y which is not what I want.

Thanks

https://stackoverflow.com/questions/67443443/merging-dataframes-and-keeping-columns-in-place May 08, 2021 at 10:05AM

没有评论:

发表评论