Pandas merge creates unwanted duplicate entries

后端 未结 4 2133
天涯浪人
天涯浪人 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:19

    import pandas as pd
    
    dict1 = {'A':[2,2,3,4,5]}
    dict2 = {'A':[2,2,3,4,5]}
    
    df1 = pd.DataFrame(dict1).reset_index()
    df2 = pd.DataFrame(dict2).reset_index()
    
    df = df1.merge(df2, on = 'A')
    df = pd.DataFrame(df[df.index_x==df.index_y]['A'], columns=['A']).reset_index(drop=True)
    
    print(df)
    

    Output:

       A
    0  2
    1  2
    2  3
    3  4
    4  5
    

提交回复
热议问题