我正在尝试删除散布在我的pandas列中这一个单元格中的转义码。我需要对整个列执行代码,但是一个单元格阻碍了整个列。
调用特定单元格的代码如此;
df.topics[0]
输出
'[{\'urlkey \':\'witi \',\'name \':\'Women in Technology \',\'id \':10296},{\'urlkey \':\'交叉指导与专家 - 首席执行官 - 所有者\',''名称:'''与专家CEO企业主交叉指导',''id \':15145},{\'urlkey \': '创业',''名称':''企业家',''id \':19882},{\'urlkey \':\'womens-business-networking \',\'name \': “女性的商业网络”,“id”:21283},{\'urlkey \':\'startup-businesses \',\'name \':\''Startup Businesses','\ id \ ':21681},{\'urlkey \':\'lean-startup \',\'name \':\'Lean Startup \',\'id \':38660},{\'urlkey \':\ '女企业家',''名称:'''女企业家',''id \':41905},{\'urlkey \':''创始人',''名称':'\'创始人\',\'id \':46616},{\'urlkey \':\'technology-startups \',\'name \':\'Technology Startups \',\'id \':108403}, {\'urlkey \':\'ceo-2-ceo-coaching-mentoring-for-mutual-growth \',''name \':''CEO 2 CEO Coaching&指导共同成长\',\'id \':133122},{\'urlkey \':\'cto \',\'name \':\'CTO \',\'id \':141917}, {\'urlkey \':\'ceo \',\'name \':\'CEO \',\'id \':141921},{\'urlkey \':\'c-level-tech \' ,''name \':\'C-Level Tech \',\'id \':816562},{\'urlkey \':\'ceos-founders \',\'name \':\'CEO& amp; ; Founders \',\'id \':1379732},{\'urlkey \':\'cio-cto \',\'name \':\'CIO / CTO \',\'id \':1485582} ]'`
而其他细胞看起来像这样;
[{'urlkey':'opensource','name':'开源','id':563},{'urlkey':'ebizowners','name':'电子商务所有者',' id':1330},{'urlkey':'softwaredev','name':'Software Development','id':3833},{'urlkey':'socialnetwork','name':'Social Networking',' id':4422},{'urlkey':'网络','名称':'网络技术','id':10209},{'urlkey':'技术','名称':'技术','id ':10579},{'urlkey':'在线营销','名称':'在线营销','id':15585},{'urlkey':'数字媒体','名称':'数字媒体','id':17188}]
你能用我的代码来帮我删除\(我认为换行符)所以所有单元格都相似。
不确定它是否仅适用于第一个细胞,但其余部分看起来正常。但是,为了以防万一,删除整列的代码将非常有用。
感谢。
答案 0 :(得分:1)
我想说df.apply(lambda x:x.replace(“\'”,''))应该可以胜任。
但是对于更复杂的东西,pandas有.replace方法:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html
答案 1 :(得分:1)
您可以使用模块ast
评估单元格的内容:
import ast
>>> ast.literal_eval(s)
[{'id': 10296, 'name': 'Women in Technology', 'urlkey': 'witi'},
{'id': 15145,
'name': 'Cross Mentoring with expert CEO business owners',
'urlkey': 'cross-mentoring-with-expert-ceo-business-owners'},
{'id': 19882, 'name': 'Entrepreneurship', 'urlkey': 'entrepreneurship'},
{'id': 21283,
'name': "Women's Business Networking",
'urlkey': 'womens-business-networking'},
{'id': 21681, 'name': 'Startup Businesses', 'urlkey': 'startup-businesses'},
{'id': 38660, 'name': 'Lean Startup', 'urlkey': 'lean-startup'},
{'id': 41905,
'name': 'Female Entrepreneurs',
'urlkey': 'female-entrepreneurs'},
{'id': 46616, 'name': 'Founders', 'urlkey': 'founders'},
{'id': 108403,
'name': 'Technology Startups',
'urlkey': 'technology-startups'},
{'id': 133122,
'name': 'CEO 2 CEO Coaching & Mentoring For Mutual Growth',
'urlkey': 'ceo-2-ceo-coaching-mentoring-for-mutual-growth'},
{'id': 141917, 'name': 'CTO', 'urlkey': 'cto'},
{'id': 141921, 'name': 'CEO', 'urlkey': 'ceo'},
{'id': 816562, 'name': 'C-Level Tech', 'urlkey': 'c-level-tech'},
{'id': 1379732, 'name': 'CEOs & Founders', 'urlkey': 'ceos-founders'},
{'id': 1485582, 'name': 'CIO / CTO', 'urlkey': 'cio-cto'}]
如果您删除"\'"
,则无法返回词典列表。要删除"\'"
,只需应用于字符串s.replace("\'","")
即可。我认为由于具有不同的econdigs,字符串可能不同。