我有这个数据集:
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
我已经尝试过这个answer的解决方案以及代码
df.sort_values(by=["Scores"], na_position="first")
但是结果并不是想要的。
当你的列中有对象时,你需要循环:
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