I have a DataFrame df1
that looks like this:
我有一个看起来像这样的DataFrame df1:
A B C
-----------------
1 1 2
2 2 3
5 4 9
I want to get all the unique values in a row. For example 1 and 2 in the first row. 2, 3 in the second row. And 4, 5 and 9 in the third row.
我希望连续获得所有唯一值。例如,第一行中的示例1和2。 2,3排在第二排。第三排是4,5和9。
Result can vary, I can imagine a new column that contains a list with unique values or replacing duplicates with None
would be also okay (or something else, maybe there is something more pythonic for this case).
结果可能会有所不同,我可以想象一个包含具有唯一值的列表或用None替换重复项的新列也可以(或者其他东西,也许这种情况下有更多的pythonic)。
3 个解决方案
#1
13
list(map(set,df.values))
Out[72]: [{1, 2}, {2, 3}, {4, 5, 9}]
#2
6
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
#3
6
Lets use pd.unique
i.e
让我们使用pd.unique即
df.T.agg([pd.unique])
0 1 2
unique [1, 2] [2, 3] [5, 4, 9]
#1
13
list(map(set,df.values))
Out[72]: [{1, 2}, {2, 3}, {4, 5, 9}]
#2
6
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
#3
6
Lets use pd.unique
i.e
让我们使用pd.unique即
df.T.agg([pd.unique])
0 1 2
unique [1, 2] [2, 3] [5, 4, 9]