簡體   English   中英

如何刪除熊貓列中的單引號

[英]How to remove single quotation marks in a column on pandas

如何在我的數據框中刪除 Genre 和 Country 列的單引號?

         ID                    Title                Type    Year  Released   Genre          Country
0   ts300399    Five Came Back: The Reference Films SHOW    1945    48     'documentary'      'US'

我嘗試了 strip() 但沒有用。

df[['Genre', 'Country']] = df[['Genre', 'Country']].apply(lambda col: col.str.strip("'"))
print(df)

   ID     Title                                      Type  Year  Released        Genre Country
0   0  ts300399  Five Came Back: The Reference Films SHOW  1945        48  documentary      US
df[['Genre','Country']] = df[['Genre','Country']].replace({"^'|'$": ""}, regex=True)

編輯

.replace方法似乎比.apply() + .strip()方法快一點:

在此處輸入圖像描述

假設單引號始終存在:

df[['Genre', 'Country']] = df[['Genre', 'Country']].applymap(lambda x: x[1:-1])

會完成工作並且速度非常快。

選擇感興趣的列並將標點符號替換為“”

df[['Genre','Country']] = df[['Genre','Country']].replace({"$^'|'": ""}, regex=True)

暫無
暫無

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

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