从Dataframe中的2个或更多列获取唯一值的有效方法

alv*_*vas 13 python csv dataframe pandas sframe

给出一个矩阵SFrame:

>>> from sframe import SFrame
>>> sf =SFrame({'x':[1,1,2,5,7], 'y':[2,4,6,8,2], 'z':[2,5,8,6,2]})
>>> sf
Columns:
    x   int
    y   int
    z   int

Rows: 5

Data:
+---+---+---+
| x | y | z |
+---+---+---+
| 1 | 2 | 2 |
| 1 | 4 | 5 |
| 2 | 6 | 8 |
| 5 | 8 | 6 |
| 7 | 2 | 2 |
+---+---+---+
[5 rows x 3 columns]
Run Code Online (Sandbox Code Playgroud)

我想获得xy列的唯一值,我可以这样做:

>>> sf['x'].unique().append(sf['y'].unique()).unique()
dtype: int
Rows: 7
[2, 8, 5, 4, 1, 7, 6]
Run Code Online (Sandbox Code Playgroud)

这样,我得到x的唯一值和y的唯一值,然后追加它们并获得附加列表的唯一值.

我也可以这样做:

>>> sf['x'].append(sf['y']).unique()
dtype: int
Rows: 7
[2, 8, 5, 4, 1, 7, 6]
Run Code Online (Sandbox Code Playgroud)

但是这样,如果我的x和y列很大并且有很多重复,我会在获得唯一之前将它附加到一个非常大的容器中.

有没有更有效的方法来获取从SFrame中的2个或更多列创建的组合列的唯一值?

大熊猫中有两种或更多列获得唯一值的有效方法是pandas什么?

Mer*_*lin 2

我没有 SFrame,但在 pd.DataFrame 上进行了测试:

  sf[["x", "y"]].stack().value_counts().index.tolist()
  [2, 1, 8, 7, 6, 5, 4]
Run Code Online (Sandbox Code Playgroud)