将列表的熊猫列拆分为字符串存储到多行(literal_eval错误)

时间:2018-10-17 20:03:42

标签: python pandas dataframe

我有一个数据框,其中一列列表存储为字符串。列表的大小各不相同。我想将列表中的项目拆分为多行

df['urls']
0    '[http://sth/sth]'
1    '[http://sth.COM, https://twitter.com/i/etc]'
2    '[]'

尝试使用literal_eval()函数:

df['urls'] = df['urls'].map(lambda x: literal_eval(x))

但出现以下错误:

File "<unknown>", line 1
[http://sth/sth]
     ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:1)

要仅将网址保留为文本,可以使用strip(),然后使用split()

s = '[http://sth.COM, https://twitter.com/i/etc]'
s = s.strip('[]') # this will remove the [] at the beginning and ending of the string.
urls = s.split(',') # this will separate both urls, returning a list of strings. ['http://sth.COM', ' https://twitter.com/i/etc']

您也可以使用切片进行剥离:

s = '[http://sth.COM, https://twitter.com/i/etc]'
s = s[1:-1] # this will keep everything except first and last characters, in this case [ and ].
urls = s.split(',') # this will separate both urls, returning a list of strings. ['http://sth.COM', ' https://twitter.com/i/etc']

希望对您有帮助!