0

for i in data['test preparation course']: if i == 'none': i = None

here, I'm trying to convert 'none' string with None values in python, and it went well.

I just want to activate the changes on the dataset

  • Assigning to `i` does not change `data`; it just makes the name `i` refer to somethign else. If you want to change what `data['...']` refers to, you need to assign directly to `data['...']`. (Read https://nedbatchelder.com/text/names.html.) – chepner May 13 '23 at 20:43
  • using a loop in pandas is an anti pattern . Can you tell us what you're trying to do and we perhaps help you more. – Umar.H May 13 '23 at 20:51
  • I'm just trying to convert "none" as a string to None Python object (like: np.nan or something like that) – W.xtar777 May 14 '23 at 21:25

1 Answers1

2

Instead, use pandas.Series.replace function:

data['test preparation course'].replace('none', None, inplace=True)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • don't use [`inplace =True`](https://stackoverflow.com/a/59242208/9375102) – Umar.H May 13 '23 at 20:50
  • 1
    @ScottBoston the linked post has more info - tldr, no performance gains, and does not stop and does not stop copies of your dataframe from being created. – Umar.H May 16 '23 at 20:45