代码之家  ›  专栏  ›  技术社区  ›  snow_fall

从pandas列中的特定单元格中删除转义码

  •  0
  • snow_fall  · 技术社区  · 6 年前

    我正在尝试删除散布在我的熊猫专栏的这一个单元格中的转义码。我需要对整个列执行代码,但这一个单元格妨碍了所有列的执行。

    调用特定单元格的代码如下所示; df.topics[0]

    输出

    “[{urlkey\':'witi\,'name\':'Women in Technology\,'id\:10296},{urlkey\':'cross-mentering-with-expert-ceo-business-owners\,'name\:'cross-mentering-with-expert-ceo-business-owners\,'id\':'ceo-business-owners\,'id\,'15145},{urlkey\':'entrepreneurship\,'name\:'entrepreneurship\,'id\:19882},{urlkey\:'womens-business-networking\”“,\'name\”:“女性商业网络”,\'id\:21283},{'urlkey\:\'startup-Business\,\'name\:\'startup-Business\,\'id\:21681},{'urlkey\:'lean-startup\,\'name\:'lean-startup\,\'id\:38660},{'urlkey\:'Femal-Enterprisers\,\'name\:'Femal Enterprisers\,\'id\:41905},{'urlkey\:'founders\,\'name\'“:\'founders\,\'id\':46616},{urlkey \':\'technology-startups \'、\'name \':\'technology-startups \'、\'id \':108403}、{urlkey \':'ceo-2-ceo-coaching-mentering-for-mutual-growth \'、\'name \':'ceo-2-ceo-ceo-ceo-coaching-mentering-for-mutual-growth \'、\'id \':'ceo-2-ceo-ceo-coaching-menting-for-mutual-growth \'、'id \':'141917}、{urlkey \':\'ceo\,\'name\:\'ceo\,\'id\:141921},{'urlkey\':'c-level-tech\','name\':'c-level-tech\,'id\':816562},{'urlkey\':'ceos-founders\,'name\':'ceos&founders\,'id\':1379732},{'urlkey\':'cio-cto\,'name\':'cio/cto\,'id\':1485582}`

    而其他细胞看起来是这样的;

    [{'urlkey':'opensource','name':'Open Source','id':563},{'urlkey':'ebizowners','name':'E-Business Owners','id':1330},{'urlkey':'softwaredev','name':'Software Development','id':3833},{'urlkey':'socialnetwork','name':'Social Networking','id':4422},{'urlkey':'web','web Technology','id':10209},{'urlkey':'Technology','name','Technology','id':'10579},{'urlkey':'在线营销','name':'在线营销','id':15585},{'urlkey':'数字媒体','name':'数字媒体','id':17188}]

    你能帮我用代码删除\(我想换行符),这样所有的单元格都是相似的吗。

    不确定是否只针对第一个细胞,它可能在其他细胞中,但其他细胞看起来正常。然而,为整个列删除代码以防万一将非常有用。

    谢谢

    2 回复  |  直到 6 年前
        1
  •  1
  •   Mabel Villalba    6 年前

    您可以通过使用模块对单元格进行求值来检索单元格的内容 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("\'","") 。我想可能是因为有不同的经济数字,字符串是不同的。

        2
  •  1
  •   IWHKYB    6 年前

    我会说df。apply(lambda x:x.replace(“\”,“”))应执行此操作。

    但对于更复杂的东西,熊猫有。替换方法: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html