Pandas: unique dataframe

前端 未结 2 918
半阙折子戏
半阙折子戏 2020-12-08 18:59

I have a DataFrame that has duplicated rows. I\'d like to get a DataFrame with a unique index and no duplicates. It\'s ok to discard the duplicated values. Is this possible?

相关标签:
2条回答
  • 2020-12-08 19:38
    In [29]: df.drop_duplicates()
    Out[29]: 
       b  c
    1  2  3
    3  4  0
    7  5  9
    
    0 讨论(0)
  • 2020-12-08 20:00

    Figured out one way to do it by reading the split-apply-combine documentation examples.

    df = pandas.DataFrame({'b':[2,2,4,5], 'c': [3,3,0,9]}, index=[1,1,3,7])
    df_unique = df.groupby(level=0).first()
    
    df
       b  c
    1  2  3
    1  2  3
    3  4  0
    7  5  9
    
    df_unique
       b  c
    1  2  3
    3  4  0
    7  5  9
    
    0 讨论(0)
提交回复
热议问题