0

I have a python dataframe df as:

            Pkg  DateType
Date                     
2020-01-07  2.39 2020-01-07
2020-01-08  4.20 2020-01-09
2020-01-19  7.49 2020-02-01
2020-01-20  7.49 2020-03-01

I want the following:

            Pkg  DateType   DTCat
Date                     
2020-01-07  2.39 2020-01-07 NDay
2020-01-08  4.20 2020-01-01 BM 
2020-01-19  7.49 2020-02-01 NM
2020-01-20  7.49 2020-03-01 NP1M

where the column DTCat is created based on the following dictionary condition applied to column DateType

{'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'}

I am not sure how to apply the conditions to the column in the above dataframe.

Zanam
  • 4,607
  • 13
  • 67
  • 143

1 Answers1

1

Just map it:

df['DTCat'] = df['DateType'].map({'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'})
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65