Get All Unique Values in a Row

前端 未结 3 417
陌清茗
陌清茗 2021-01-12 04:58

I have a DataFrame df1 that looks like this:

A       B       C
-----------------
1       1       2
2       2       3
5       4       9


        
相关标签:
3条回答
  • 2021-01-12 05:33
    list(map(set,df.values))
    Out[72]: [{1, 2}, {2, 3}, {4, 5, 9}]
    
    0 讨论(0)
  • 2021-01-12 05:36
    In [88]: df.stack().groupby(level=0).apply(lambda x: x.unique().tolist())
    Out[88]:
    0       [1, 2]
    1       [2, 3]
    2    [5, 4, 9]
    dtype: object
    
    0 讨论(0)
  • 2021-01-12 05:48

    Lets use pd.unique i.e

    df.T.agg([pd.unique])
    
            0       1          2
    unique  [1, 2]  [2, 3]  [5, 4, 9]
    
    0 讨论(0)
提交回复
热议问题