对列内嵌套列表进行排序

Order nested lists inside column

提问人:josepmaria 提问时间:9/11/2023 更新时间:9/11/2023 访问量:33

问:

我有这个数据集:

df = pd.DataFrame({'Name':['John', 'Rachel', 'Adam','Joe'],
                   'Age':[95, 102, 31,np.nan],
                   'Scores':[np.nan, [80, 82, 78], [25, 20, 30, 60, 21],np.nan]
                  })

我想对“分数”列中的值进行排序。

所需输出:

 Name    Age     Scores
John    95.0     NaN
Rachel  102.0    [78,80,82]
Adam    31.0     [20,21,25,30,60]
Joe     NaN      NaN

我已经尝试了这个答案的解决方案,以及代码

df.sort_values(by=["Scores"], na_position="first")

但结果并不是那么理想。

Python Pandas 排序 嵌套列表

评论


答:

1赞 mozway 9/11/2023 #1

由于列中有对象,因此需要循环:

df['Scores'] = [sorted(l) if isinstance(l, list) else l for l in df['Scores']]

输出:

     Name    Age                Scores
0    John   95.0                   NaN
1  Rachel  102.0          [78, 80, 82]
2    Adam   31.0  [20, 21, 25, 30, 60]
3     Joe    NaN                   NaN

评论

0赞 josepmaria 9/11/2023
代码运行,但真实数据集中的结果列表保持无序 [4, 18, 23, 11, 26, 8, 14, 1, 30, 2, 17, 25, ...还有其他选择吗?
2赞 mozway 9/11/2023
您@josepmaria分配了输出?这真的没有意义,你能提供一个可重复的输入来做到这一点吗?
2赞 Shubham Sharma 9/11/2023 #2

删除 null 值,然后使用mapsorted

df['Scores'] = df['Scores'].dropna().map(sorted)

     Name    Age                Scores
0    John   95.0                   NaN
1  Rachel  102.0          [78, 80, 82]
2    Adam   31.0  [20, 21, 25, 30, 60]
3     Joe    NaN                   NaN

评论

2赞 mozway 9/11/2023
我犹豫要不要放这个替代方案,它看起来更漂亮,但实际上比列表理解慢(是一个循环),如果有重复的索引(尝试使用或输入),则不起作用。mapindex=[1, 1, 2, 3]index=[0, 1, 1, 2]
1赞 Shubham Sharma 9/11/2023
嗯,很高兴知道
0赞 Jesse Sealand 9/11/2023 #3

解决方案

一种方法是使用 apply lambda 循环访问每一行,从而对列表进行有条件的排序,如下所示:

df['Scores'] = df['Scores'].apply(lambda x: sorted(x) if isinstance(x, list) else x)

#   Name    Age Scores
#0  John    95.0    NaN
#1  Rachel  102.0   [78, 80, 82]
#2  Adam    31.0    [20, 21, 25, 30, 60]
#3  Joe     NaN     NaN

这还会处理缺失值,这在其他方法中会带来问题。