Home > OS >  How to remove single quotation marks in a column on pandas
How to remove single quotation marks in a column on pandas

Time:05-29

How do I remove the single quotation mark for the columns Genre and Country in my dataframe?

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

I tried strip() but didn't work.

CodePudding user response:

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

CodePudding user response:

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

Edit

The .replace method seems to be slighlty faster than the .apply() .strip() one:

enter image description here

CodePudding user response:

Assuming the single quotes are always present:

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

Would get the job done and be very fast.

CodePudding user response:

Select the columns of interest and replace the punctuations by ""

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