1

I'm trying to remove all the words not from the Dataframe below.

d = {'keep': ["not useful", "useful", "not useful", "useful", "useful"]}
df = pd.DataFrame(data=d)
df['keep'] = df['keep'].replace('not', '', inplace=True)
df

This results in my 'keep' column being equal to None. Not sure what I'm doing incorrectly here.

Thanks

notverygood
  • 297
  • 2
  • 13

2 Answers2

3

Two issues:

  1. inplace=True performs inplace operation, so it modifies the series directly and returns None.
  2. df['keep'].replace(a,b) replaces for the whole strings, not substrings.

You want:

df['keep'] = df['keep'].replace('not','', regex=True)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

Use:

for i in range(len(df['keep'])):
    df['keep'][i] = df['keep'][i].replace('not', '')
JD12
  • 94
  • 6
  • Please add an explanation highlighting the important bits of your solution, or include a documentation link, or how it differs in an important way from OP's attempt, etc. Code-only answers are frowned upon on SO, are of low quality (reflecting on the platform), tend to promote *haz me the code* Q's, and have little if any long term value. Future visitors should be able to learn from your response, and apply knowledge to their own issues. Generally, simple answers like this are more appropriately left as a Comment, and are very Welcome as Comments. Quality answers are more likely to be upvoted. – SherylHohman Jul 22 '20 at 20:19