创建基于另一列数据的类别列。

huangapple go评论114阅读模式
英文:

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'})

huangapple
  • 本文由 发表于 2020年1月6日 23:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定