0

For a column in my dataframe I am trying to replace strings to integers.

There are only two string in the column named Indicators. for ex : "A","B"

I want all "A" in the column to be set to 1. All "B" in the column be set to 0.

Tried two methods however both gives me "A value is trying to be set on a copy" Error

method 1:

df["Indicators"] = df["Indicators"].replace("A", 1)
df["Indicators"] = df["Indicators"].replace("B", 2)

outputs what I want but gives me an error. Also is there a way to put these into one line of code?

method 2:

indicators = {"A":1, "B":2}
df["Indicators"] = df["Indicators"].apply(indicators.get).astype(float)

This gives me the same thing.

haneulkim
  • 4,406
  • 9
  • 38
  • 80
  • I think your df is a subset of some other dataframe , check `.copy()` – BENY Mar 05 '19 at 19:37
  • Might I recommend taking a look at [this answer](https://stackoverflow.com/a/53954986/4909087) and adding a `df = df.copy()` line before this? – cs95 Mar 05 '19 at 19:37

0 Answers0