我不确定为什么“set”在下面的示例中没有唯一的值:
df6 = pd.DataFrame({
'Name': ['Sara', 'John'],
'one': ['UK', 'UK'],
'two': ['IN', 'SA'],
'three': ['IN', 'IN'],
'four': ['IN', 'US']
})
df6
给予:
Name one two three four
0 Sara UK IN IN IN
1 John UK SA IN US
我在列表中连接了(一到四)列:
df6['Concat'] = df6[['one','two','three','four']].apply(lambda x: [', '.join(x[x.notnull()])], axis = 1)
给予:
Name one two three four Concat
0 Sara UK IN IN IN [UK, IN, IN, IN]
1 John UK SA IN US [UK, SA, IN, US]
现在我只想在
Concat
每个名称对应的列:
我尝试了以下方法:
df6.Concat.apply(set)
但结果与原始列表相同!
0 {UK, IN, IN, IN}
1 {UK, SA, IN, US}
Name: Concat, dtype: object
为什么“set”在这种情况下不起作用?
我不想让唯一的列表有序,但只是为了提高我的学习能力,我如何才能让唯一的值有序?