Maxi Köhler
Maxi Köhler

Reputation: 23

DataFrame.replace by nested dict

I have a huge Dataframe, where a lot of entries needs to be changed. So, I've created a translation dict, which has the following structure:

{'Data.Bar Layer': {'1': 0,
  '1.E': 21,
  '2': 13,
  '2.E': 22,
  '3': 14,
  '3.E': 24,
  '4': 15,
  '4.E': 23,
  'B': 16,
  'CL1': 1,
  'CL2': 2,
  'CL2a': 6,
  'CL3': 3,
  'CL3a': 4,
  'CL4': 5,
  'E': 18,
  'L1': 7,
  'L2': 8,
  'L2a': 12,
  'L3': 9,
  'L3a': 10,
  'L4': 11,
  'T': 17,
  'T&B': 19,
  'T+B': 20},
 'Data.Bar Type': {'N': 0, 'R': 1},
 'Data.Defined No. Bars': {'No': 0, 'Yes': 1},
 'Data.Design Option': {'-1': 0, 'Main Model': 1},...}

screenshot of the dictionaries print representation

The first key corresponds to the dataframe column and the second key to the value that needs to be changed, e.g. in column Data.Bar Layer all '1' should be 0. This is how the documentation of pandas.dataframe.replace states the dictionary to look like However, same values have to be exchanged multiple times, which (I guess) leads to the error:

Replacement not allowed with overlapping keys and values

Here is a snippet of the Dataframe. Is there any work around to avoid this error? I tried some approaches with apply and map, but they didn't work, unfortunately.

Thanks in advance and kind regards, Max

Upvotes: 2

Views: 400

Answers (1)

smitsy
smitsy

Reputation: 81

There might be a more pythonic way, but this code works for me;

for col in your_dict.keys():
    df[col].replace(your_dict[col], inplace=True)

Upvotes: 1

Related Questions