pd.dataframe - 在不更改索引的情况下对列表列中的每个列表进行排序

pd.dataframe - sort each list in a column of lists without changing index

如果我有这个 pandas v1.3.4 dataframe:

index         col1          col2
  1      ['1','2','3']       'a'
  2      ['2','4','2']       'b'
  3      ['5','2','1']       'c'
  4      ['3','2','1']       'd'

如何在不更改 index 或任何其他值(在本例中为 col2)的情况下对 col1 中的每个值进行排序?对于这个例子,如果我从最低到最高排序(假设 lexographic 排序与数字排序相匹配)我会得到:

index         col1          col2
  1      ['1','2','3']       'a'
  2      ['2','2','4']       'b'
  3      ['1','2','5']       'c'
  4      ['1','2','3']       'd'

我并不特别关心我采用什么排序方法,我只是希望具有相同项目的列表具有相同的顺序,以便它们被认为是等价的,用于某些下游数据可视化。

谢谢!

蒂姆

如果 col1 是一个字符串,您可以将您的列转换为带有 ast.literal_eval 的列表,然后使用 apply:

对其进行排序
import ast
df.col1 = df.col1.apply(lambda x: sorted(ast.literal_eval(x)))
print(df)

输出:

            col1 col2
index
1      [1, 2, 3]  'a'
2      [2, 2, 4]  'b'
3      [1, 2, 5]  'c'
4      [1, 2, 3]  'd'

如要对整数的字符串表示形式进行排序,请使用 natsort:

from natsort import natsorted
df['col1'] = df['col1'].apply(natsorted)

输出:

   index             col1 col2
0      1  ['1', '2', '3']  'a'
1      2  ['2', '2', '4']  'b'
2      3  ['1', '2', '5']  'c'
3      4  ['1', '2', '3']  'd'

或者很好的旧列表理解。

df['col1'] = [sorted(i) for i in df.col1]

使用 iris 的示例:

iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
iris['test'] = iris[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].values.tolist()
iris['test2'] = [sorted(i) for i in iris.test]

如果您不想使用任何导入(当然 pandas 除外):

import pandas as pd
df = pd.DataFrame({'col1': [['1', '2', '20'], ['2', '10', '2'], ['30', '2', '1'], ['3', '2', '1']]})

您可以使用以下方式对每个列表进行数字排序:

df[['col1']].apply(lambda x: sorted(map(int,x["col1"])), axis=1)

输出

0    [1, 2, 20]
1    [2, 2, 10]
2    [1, 2, 30]
3     [1, 2, 3]

或作为字符串使用:

df[['col1']].apply(lambda x: sorted(map(str,x["col1"])), axis=1)

输出

0    [1, 2, 20]
1    [10, 2, 2]
2    [1, 2, 30]
3     [1, 2, 3]