![](/img/trans.png)
[英]Python list of strings to 1 string separated by comma & quotes (, & ")
[英]Python explode strings with comma and quotes
我有一個具有以下設置的數據集:
id string_to_parse
1 "a","b"
2 "a,b","c"
3 "c"
我需要把它放進去
id string_to_parse
1 a
1 b
2 a,b
2 c
3 c
我試過
exploded_ = df['string_to_parse'].map(lambda x:x\
.replace('"','')\
.split(",")).explode()
除了非常慢之外,它還錯過了"a,b"
並將它們分開。
使用Series.str.strip
和Series.str.split
最后一個DataFrame.explode
:
df['string_to_parse'] = df['string_to_parse'].str.strip('"').str.split('","')
df = df.explode('string_to_parse')
print (df)
id string_to_parse
0 1 a
0 1 b
1 2 a,b
1 2 c
2 3 c
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.