0

Is it possible to apply a mask to a dataframe column in the moment of it's creation?

I have the following scenario:

Name   Age   Event
Ian    23    1
Ana    26    2
Mark   54    2
John   15    3

Each event value corresponds to a friendly name which is stored in a dict

event_types = {
  1: 'Event 1',
  2: 'Event 2',
  3: 'Event 3'
}

And I want to replace this the event values from the dataframe to its corresponding value from the dict

What I have already tried this along with other variants, unsuccessfully

  for event_type in event_types:
        data.filter(['event_type']).mask(data == event_type, other=event_types[event_type])

I achieved the expected result using .loc and lambda but had performance issues, because the DataFrame contains millions of records

Any ideas how can I change the event column values for it's friendly name?

Ian Guimarães
  • 371
  • 3
  • 12
  • 1
    did you try map/replace? `df['Event']=df['Event'].map(event_types).fillna(df['Event'])` or `df['Event']=df['Event'].replace(event_types)` – anky Mar 05 '20 at 14:29
  • df['Event']=df['Event'].replace(event_types) was it. Make it an answer!! – Ian Guimarães Mar 05 '20 at 14:46

0 Answers0