将字符串列表转换为pandas中的int列表



我的数据如下:

{569328: '[  596005  4321416  5802640  6031690  6043910  8600475  8642629  9203255  9345445 10177065 10455451 13428248 22139349 22591458 24627241 24750476 26261826 26405611 27079105 27096884]',
574660: '[ 5956195 11260528 22181831 22437920 22642946 23278096 23407037 23458128 24244657 24355363 25014714 25115774 25156886 27047688 27089078 27398716]',
1187498: '[ 5855196  7755392 11183886 22894980 24648618 27185399]',
1226468: '[ 3573464  6279285  6294985  6542463  6981930  7427770 10325811 14970234 16878329 17935009 21811002 22329817 23543436 23907898 24456108 25283772]',
1236571: '[ 2777078  2826073  5944733 10484188 11052747 14682645 15688752 22333410 22614097 22646501 22783765 22978728 23231683 24259740 24605606 24839432 25492752 27009992 27044704]'}

正如您所看到的,dict的值是字符串,dict是我的pandas-df的一列。不过,我想把它们转换成适当的清单。我的结果应该是这样的:

{569328: [596005, 4321416,5802640,6031690,6043910,8600475,8642629,9203255,9345445, 10177065,10455451,13428248,22139349,22591458,24627241,24750476,26261826,26405611, 27079105,27096884],
574660: [5956195,11260528,22181831,22437920,22642946,23278096,23407037,23458128, 24244657,24355363,25014714,25115774,25156886,27047688,27089078,27398716],
...}

谢谢:(

stripsplit使用dict理解,最后将列表转换为整数,因为空列表添加了if-else语句:

d = {569328: '[  596005  4321416  5802640  ]',
574660: '[ 5956195 ]',
1187498: []}

d = {k: list(map(int, v.strip('[]').split())) if bool(v) else [] for k, v in d.items()}
print (d)
{569328: [596005, 4321416, 5802640], 574660: [5956195], 1187498: []}

另一种可能的解决方案,基于以下想法:

  1. 将数据用作字符串
  2. 使用字符串操作和正则表达式删除'并添加缺少的逗号
  3. 使用eval执行被操纵的字符串,并在x中得到结果
import re  
text = """
{569328: '[  596005  4321416  5802640  6031690  6043910  8600475  8642629  9203255  9345445 10177065 10455451 13428248 22139349 22591458 24627241 24750476 26261826 26405611 27079105 27096884]',
574660: '[ 5956195 11260528 22181831 22437920 22642946 23278096 23407037 23458128 24244657 24355363 25014714 25115774 25156886 27047688 27089078 27398716]',
1187498: '[ 5855196  7755392 11183886 22894980 24648618 27185399]',
1226468: '[ 3573464  6279285  6294985  6542463  6981930  7427770 10325811 14970234 16878329 17935009 21811002 22329817 23543436 23907898 24456108 25283772]',
1236571: '[ 2777078  2826073  5944733 10484188 11052747 14682645 15688752 22333410 22614097 22646501 22783765 22978728 23231683 24259740 24605606 24839432 25492752 27009992 27044704]'}
"""
s = re.sub('(?<=d)s+(?=d)', ',', text.replace("'", ""))
x = eval(s)
x 

输出:

{569328: [596005, 4321416, 5802640, 6031690, 6043910, 8600475, 8642629, 9203255, 9345445, 10177065, 10455451, 13428248, 22139349, 22591458, 24627241, 24750476, 26261826, 26405611, 27079105, 27096884], 
574660: [5956195, 11260528, 22181831, 22437920, 22642946, 23278096, 23407037, 23458128, 24244657, 24355363, 25014714, 25115774, 25156886, 27047688, 27089078, 27398716],
1187498: [5855196, 7755392, 11183886, 22894980, 24648618, 27185399], 
1226468: [3573464, 6279285, 6294985, 6542463, 6981930, 7427770, 10325811, 14970234, 16878329, 17935009, 21811002, 22329817, 23543436, 23907898, 24456108, 25283772], 
1236571: [2777078, 2826073, 5944733, 10484188, 11052747, 14682645, 15688752, 22333410, 22614097, 22646501, 22783765, 22978728, 23231683, 24259740, 24605606, 24839432, 25492752, 27009992, 27044704]}

您可以使用regex表达式来表示数字,然后将字符串形式的数字列表转换为整数列表:

import re
dictionary = {569328: '[  596005  4321416  5802640  6031690  6043910  8600475  
8642629  9203255  9345445 10177065 10455451 13428248 22139349 
22591458 24627241 24750476 26261826 26405611 27079105 27096884]',
574660: '[ 5956195 11260528 22181831 22437920 22642946 23278096 
23407037 23458128 24244657 24355363 25014714 25115774 25156886 
27047688 27089078 27398716]',
1187498: '[ 5855196  7755392 11183886 22894980 24648618 27185399]',
1226468: '[ 3573464  6279285  6294985  6542463  6981930  7427770 
10325811 14970234 16878329 17935009 21811002 22329817 23543436 
23907898 24456108 25283772]',
1236571: '[ 2777078  2826073  5944733 10484188 11052747 14682645 
15688752 22333410 22614097 22646501 22783765 22978728 23231683 
24259740 24605606 24839432 25492752 27009992 27044704]'}
for key in dictionary:
list_of_findings = list(re.findall('d+', dictionary[key]))
dictionary[key] = list(map(int, list_of_findings))

最新更新