【问题标题】:Sort pandas column of lists and call other columns对列表的熊猫列进行排序并调用其他列
【发布时间】:2020-08-26 20:56:22
【问题描述】:

我已经看过几篇关于此的帖子,但我尝试的每个解决方案要么得到错误,要么帖子与我需要的略有不同。请参阅下面的尝试。

数据框

col1
[0.7, 0.8, -0.9]
[0.3, 0.7, 0.1]

我希望像这样按升序对该列中每个列表中的元素进行排序:

results
[-0.9, 0.7, 0.8]
[0.1, 0.3, 0.7]

尝试

#attempt1
#sorts the column based on the first element in the list
#also tried grabbing other columns with [['col1','col2']] at the end but that doesn't work
sorted(df['jaro_sequencer_diff'])

#attempt2
df['col1'].head().sorted(reverse=True)

#AttributeError: 'Series' object has no attribute 'sorted'

有什么想法吗?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用sorted:

    df['col1'].apply(sorted)
    #or
    df['col1'] = [sorted(l) for l in df['col1']]
    

    【讨论】:

    • nvm - 我的错误。这很好用。我今天需要停止工作!
    猜你喜欢
    • 2021-09-21
    • 2012-08-21
    • 2018-11-22
    • 2021-09-01
    • 2014-12-29
    • 2018-03-28
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多