[英]Sort list of string numbers that are in a column of a data frame
I have a data frame whose one column contains lists of string number我有一个数据框,其一列包含字符串编号列表
Col1
['1']
['1']
['1','3','4']
['2','3','1','4','5']
How can I sort this number?我怎样才能对这个数字进行排序? I have tried to adapt the answer given here .
我试图调整这里给出的答案。
I would like to have a sorted list of integers instead of strings.我想要一个排序的整数列表而不是字符串。
Use:利用:
In [599]: df['Col1'] = df.Col1.apply(lambda x: sorted(map(int, x)))
In [600]: df
Out[600]:
Col1
0 [1]
1 [1]
2 [1, 3, 4]
3 [1, 2, 3, 4, 5]
Try this尝试这个
df = pd.DataFrame({'Col1':[['1'],['1'],['1','3','4'],['2','3','1','4','5']]})
# use a list comprehension in which map list elements to int and sort the list
df['sorted'] = [sorted(map(int, row)) for row in df['Col1']]
print(df)
Col1 sorted
0 [1] [1]
1 [1] [1]
2 [1, 3, 4] [1, 3, 4]
3 [2, 3, 1, 4, 5] [1, 2, 3, 4, 5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.