英文:
Create category column based on data from another column
问题
Sure, here's the translated code portion:
我有一个名为df的Python数据框,如下所示:
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'}
我不确定如何将条件应用于上述数据框中的列。
Is there anything else you'd like to know or any specific help you need with this code?
英文:
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.
答案1
得分: 1
Sure, here's the translated code:
df['DTCat'] = df['DateType'].map({'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'})
英文:
Just map it:
df['DTCat'] = df['DateType'].map({'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论