簡體   English   中英

從熊貓列中的特定單元格中刪除轉義碼

[英]Removing escape codes from a specific cell in a pandas column

我正在嘗試刪除在熊貓列中散布在整個一個單元格中的轉義碼。 我需要對整個列執行代碼,但是一個單元格阻礙了所有人。

調用特定單元格的代碼如下所示; df.topics[0]

輸出量

'[{\\'urlkey \\':\\'witi \\',\\'name \\':\\'科技領域的女性',\\'id \\':10296},{\\'urlkey \\':\\'交叉指導-with-expert-ceo-business-owners \\',\\'name \\':\\'與專家CEO企業所有者的跨界指導\\',\\'id \\':15145},{\\'urlkey \\':\\'企業家精神\\',\\'name \\':\\'Entrepreneurship \\',\\'id \\':19882},{\\'urlkey \\':\\'womens-business-networking \\',\\'name \\':“ Women \\ 's Business Networking”,\\'id \\':21283},{\\'urlkey \\':\\'startup-businesses \\',\\'name \\':\\'Startup Businesses \\',\\'id \\':21681 },{\\'urlkey \\':\\'lean-startup \\',\\'name \\':\\'精益啟動\\',\\'id \\':38660},{\\'urlkey \\':\\'female-企業家\\',\\'姓名\\':\\'女企業家\\',\\'id \\':41905},{\\'urlkey \\':\\'founders \\',\\'name \\':\\'Founders \\' ,\\'id \\':46616},{\\'urlkey \\':\\'technology-startups \\',\\'name \\':\\'Technology Startups \\',\\'id \\':108403},{\\' urlkey \\':\\'ceo-2-ceo-coaching-menting-for-mutual-growth',\\'name \\':\\'CEO 2 CEO Coaching&Mentoring for Mutual Growth \\',\\'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 \\':\\'CEOs and Founders \\',\\'id \\':1379732},{\\'urlkey \\':\\'cio-cto \\',\\'name \\':\\'CIO / CTO \\',\\'id \\':1485582}]'`

而其他單元格看起來像這樣;

[{'urlkey':'opensource','name':'Open Source','id':563},{'urlkey':'ebizowners','name':'電子商務所有者','id': 1330},{'urlkey':'softwaredev','name':'Software Development','id':3833},{'urlkey':'socialnetwork','name':'Social Networking','id': 4422},{'urlkey':'web','name':'Web Technology','id':10209},{'urlkey':'technology','name':'Technology','id':10579 },{'urlkey':'online-marketing','name':'Online Marketing','id':15585},{'urlkey':'digital-media','name':'Digital Media',' id':17188}]

您能否通過代碼幫助我刪除\\(我認為換行符),以便所有單元格都相似。

不知道它是否僅適用於第一個單元格,但其他單元格看起來很正常。 但是,為防萬一,刪除整個列的代碼將非常有幫助。

謝謝。

我會說df.apply(lambda x:x.replace(“ \\'”,``)))應該可以完成這項工作。

但對於更復雜的內容,pandas具有.replace方法: https : //pandas.pydata.org/pandas-docs/stable/generation/pandas.DataFrame.replace.html

您可以通過使用模塊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("\\'","") 我認為由於使用不同的摘要,字符串可能有所不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM