I want to perform a merge in pandas on more than one column, where one of the columns is an index column.
Here are example dataframes:
df1 = pd.DataFrame(np.random.rand(4,4), columns=list('ABCD'))
df2 = pd.DataFrame(np.random.rand(4,4), columns=list('EFGH'), index= [5,2,4,1])
df1['E'] = ['hello','hello','hello','world']
df2['E'] = ['world','world','hello','hello']
I want to perform an inner merge on the index and column E, so that it will return only one row:(index,E) = (1,'hello').
what about this?
In [82]: pd.merge(df1.reset_index(), df2.reset_index(), on=['index','E']).set_index('index')
Out[82]:
A B C D E F G H
index
1 0.516878 0.56163 0.082839 0.420587 hello 0.62601 0.787371 0.121979
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.