merging dataframes (python pandas)
merging dataframes (python pandas)
I have two dataframes df1 df1 and df2 df2
I want to merge them using python pandas without creating the Cartesian product.Sample output would look like this output How should I do it?
Currently,I am using
df3=pd.merge(df1,df2,on='id',how='left') but it's giving me cross product.The resultant dataframe df3 contains 14 records 6 for id=1 and 8 for id=2.
Thanks,
1 Answer
1
You may need an additional key for help, create by cumcount
cumcount
df1['Helpkey']=df1.groupby('id').cumcount()
df2['Helpkey']=df2.groupby('id').cumcount()
df1.merge(df2,how='left').drop('Helpkey',1)
Thank You!! It worked when I replaced how='left' with how='outer'..I really appreciate your help
– RSK
Sep 18 '18 at 16:23
@RohanKulkarni if this work would you like accept it ? check mark at the left
– Wen-Ben
Sep 18 '18 at 16:24
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Welcome to StackOverflow! Please do not use pictures in your questions. You can read about how to ask a question (particularly how to create a good example) in order to get good responses.
– Alex
Sep 18 '18 at 16:17