-1

I have a table (dataframe) where multiple string columns contain curly brackets and apostrophes, which I am trying to replace with a blank space:

Table

I started with the genre1 column but I keep getting a key error.

This is the code I am trying to remove curly bracket:

movies['genre1'].replace(to_replace=["}"],value="",inplace=True)
smci
  • 32,567
  • 20
  • 113
  • 146
Norbert
  • 13
  • 2
  • 3
    Hello Norbert! Could you please paste your sample code & data, You should probably do that as some people might downvote this question and causing you not to ask questions anymore , this is basically what STACKOVERFLOW is. Thank you! – Ice Bear Dec 24 '20 at 09:53
  • 1
    I posted diagram of table, thanks for your message! – Norbert Dec 24 '20 at 10:04
  • Great! that's nice :) let's just hope those downvotes will be undo by whoever did that :) – Ice Bear Dec 24 '20 at 10:14
  • 1
    Please do not post data/code/error messages as images. Post the code/data directly here on SO. I suggest also reading [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Mr. T Dec 24 '20 at 10:34
  • 1
    Does this answer your question? [Replace multiple substrings in a Pandas series with a value](https://stackoverflow.com/questions/49413005/replace-multiple-substrings-in-a-pandas-series-with-a-value) – Mr. T Dec 24 '20 at 10:38
  • Yes let's hope! Question is solved as well! Thank you for everyone's help – Norbert Dec 24 '20 at 10:55
  • I edited your question title and body to help make it clear why this is not a duplicate, and why it's worth leaving open and not downvoting. – smci Dec 24 '20 at 10:57

2 Answers2

0

This will work!

movies['genre1'] = movies['genre1'].str.replace("}","")

What you are doing is will look exactly for '}', and wherever the value of the cell will be exactly equal to }, it will replace. So, you need to use the attr str.

  • I made an update, there was typoo before. Re-run and see if you face the error again then add error details' screenshot. – Muhammad Junaid Haris Dec 24 '20 at 10:05
  • yes it worked thank you!! now to remove the apostrophe, I have to use the same code but replace curly brackets with apostrophe right? – Norbert Dec 24 '20 at 10:16
0

You can also use replace with a key-value pair -

replace_map = {'}':'',']':''}

genre_columns = table.columns[table.columns.str.contains('genre')]

table.loc[:,genre_columns] = table.replace(genre_columns)
Vaebhav
  • 4,672
  • 1
  • 13
  • 33