简体   繁体   中英

Pandas - A merge operation that merges common columns

I have two dataframe both has columns: url , title
I want to merge them using left join on url column, but I want operation to not create different columns ( title_x , title_y ) and Instead create a single title column
Should I approach join here or np.where() like method

I tried this but I think it's not a good approach to solve the problem:

test = main.merge(df, how = 'left', on = 'url')
test['title'] = np.where(test.title_x.isna(), test.title_y, test.title_x)
test.drop(columns = ['title_x', 'title_y'], inplace=True)

You can use the column name in df to consider only the columns you want like this:

test = main.merge(df['url'], how='left', on='url')

This will consider only 'url' column from df and prevent _x and _y even if you have more columns with same names in both dataframes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM