Pandas merge creates unwanted duplicate entries

后端 未结 4 2130
天涯浪人
天涯浪人 2021-02-08 10:45

I\'m new to Pandas and I want to merge two datasets that have similar columns. The columns are going to each have some unique values compared to the other column, in addition to

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 11:15

    I have unfortunately stumbled upon a similar problem which I see is now old. I solved it by using this function in a different way, applying it to the two original tables, even though there were no duplicates in these. This is an example (I apologize, I am not a professional programmer):

    import pandas as pd
    
    dict1 = {'A':[2,2,3,4,5]}
    dict2 = {'A':[2,2,3,4,5]}
    
    df1 = pd.DataFrame(dict1)
    df1=df1.drop_duplicates()
    
    df2 = pd.DataFrame(dict2)
    df2=df2.drop_duplicates()
    
    df=pd.merge(df1,df2)
    print('df1:')
    print( df1 )
    
    print('df2:')
    print( df2 )
    
    print('df:')
    print( df )
    

提交回复
热议问题