根据另一列的数据创建类别列

Create category column based on data from another column

我有一个 python 数据帧 df 为:

            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

我想要以下内容:

            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

其中 DTCat 列是根据应用于列 DateType

的以下字典条件创建的
{'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'}

我不确定如何将条件应用于上述数据框中的列。

只是map它:

df['DTCat'] = df['DateType'].map({'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'})