Drop duplicate row, If contain all same value

前端 未结 1 894
广开言路
广开言路 2021-01-23 00:03

I have a Dataframe as below:

df = pd.DataFrame({\'first\' : [\'John\', \'Mary\',\'Peter\'],
                      \'last\' : [\'Mary\', \'John\',\'         


        
相关标签:
1条回答
  • 2021-01-23 00:37
    In [13]: pd.DataFrame(np.sort(df.values, axis=1), columns=df.columns).drop_duplicates()
    Out[13]:
      first   last
    0  John   Mary
    2  Mary  Peter
    

    or:

    In [18]: df.values.sort(axis=1)  # NOTE: it sorts DF in-place
    
    In [19]: df
    Out[19]:
      first   last
    0  John   Mary
    1  John   Mary
    2  Mary  Peter
    
    In [20]: df.drop_duplicates()
    Out[20]:
      first   last
    0  John   Mary
    2  Mary  Peter
    
    0 讨论(0)
提交回复
热议问题