Home > Software engineering >  How do I replace values in data column according to dictionary?
How do I replace values in data column according to dictionary?

Time:01-27


I need to replace the values in a column of my dataset with values that refer to the values with the same name in dictionary.
So my data looks like this (don't pay attention on column names):
data = data[['A?',
       'B',...,]]

And the dictionary looks like ths:

felt_index = {
  "First opt": 1,
  "Second opt": 1,
  "Third opt": 0.72
}

I want that instead of my column looks like this:

A? ...
First opt ...
Second opt ...
Third opt ...

It would have look like this:

A? ...
1 ...
1 ...
0.72 ...

I've tried some solutions and it didn't work out. The last thing I tried is:

for val in data[['A?']]:        
        data[['A']][val] = felt_index.get(data[['A?']][val])

And I got this error:

TypeError: unhashable type: 'Series'

I can't figure t out how to solve it:( Please, help.

CodePudding user response:

Use pandas.DataFrame.replace:

new_df = df.replace({"A?": felt_index})
print(new_df)

Output:

     A?  ...
0  1.00  ...
1  1.00  ...
2  0.72  ...

From the official doc of pandas.DataFrame.replace:

For a DataFrame, nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as follows: look in column 'a' for the value 'b' and replace it with NaN.

If you want to replace those not in a dictionary:

# Sample
           A?  ...
0   First opt  ...
1  Second opt  ...
2   Third opt  ...
3     Non opt  ...

df["A?"] = df["A?"].map(felt_index).replace({np.nan: None})

Output:

     A?  ...
0   1.0  ...
1   1.0  ...
2  0.72  ...
3  None  ...

Note that since you want None, it requires replace with dict. If you want a specific value, use fillna instead:

df["A?"] = df["A?"].map(felt_index).fillna("NO!")

Output:

     A?  ...
0   1.0  ...
1   1.0  ...
2  0.72  ...
3   NO!  ...
  •  Tags:  
  • Related