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

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

Create category column based on data from another column

问题

Sure, here's the translated code portion:

  1. 我有一个名为dfPython数据框如下所示
  2. Pkg DateType
  3. Date
  4. 2020-01-07 2.39 2020-01-07
  5. 2020-01-08 4.20 2020-01-09
  6. 2020-01-19 7.49 2020-02-01
  7. 2020-01-20 7.49 2020-03-01
  8. 我想要以下结果
  9. Pkg DateType DTCat
  10. Date
  11. 2020-01-07 2.39 2020-01-07 NDay
  12. 2020-01-08 4.20 2020-01-01 BM
  13. 2020-01-19 7.49 2020-02-01 NM
  14. 2020-01-20 7.49 2020-03-01 NP1M
  15. 其中`DTCat`列是基于以下字典条件对`DateType`列创建的
  16. {'2020-01-07': 'NDay', '2020-01-01': 'BM', '2020-02-01': 'NM', '2020-03-01': 'NP1M'}
  17. 我不确定如何将条件应用于上述数据框中的列

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:

  1. Pkg DateType
  2. Date
  3. 2020-01-07 2.39 2020-01-07
  4. 2020-01-08 4.20 2020-01-09
  5. 2020-01-19 7.49 2020-02-01
  6. 2020-01-20 7.49 2020-03-01

I want the following:

  1. Pkg DateType DTCat
  2. Date
  3. 2020-01-07 2.39 2020-01-07 NDay
  4. 2020-01-08 4.20 2020-01-01 BM
  5. 2020-01-19 7.49 2020-02-01 NM
  6. 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

  1. {'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:

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

Just map it:

  1. 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:

确定