为了账号安全,请及时绑定邮箱和手机立即绑定

从日期列创建月份列(但是日期列不包含月份信息)

从日期列创建月份列(但是日期列不包含月份信息)

慕工程0101907 2022-06-02 11:25:03
我有这样的数据,并想创建一个名为“月”的列+---------+------------------+------+------+| Name    | Task             | Team | Date |+---------+------------------+------+------+| John    | Market study     | A    | 1    |+---------+------------------+------+------+| Michael | Customer service | B    | 1    |+---------+------------------+------+------+| Joanna  | Accounting       | C    | 1    |+---------+------------------+------+------+| John    | Accounting       | B    | 2    |+---------+------------------+------+------+| Michael | Customer service | A    | 2    |+---------+------------------+------+------+| Joanna  | Market study     | C    | 2    |+---------+------------------+------+------+| John    | Customer service | C    | 1    |+---------+------------------+------+------+| Michael | Market study     | A    | 1    |+---------+------------------+------+------+| Joanna  | Customer service | B    | 1    |+---------+------------------+------+------+| John    | Market study     | A    | 2    |+---------+------------------+------+------+| Michael | Customer service | B    | 2    |+---------+------------------+------+------+| Joanna  | Accounting       | C    | 2    |+---------+------------------+------+------+所以基本上,我有日期信息,但日期不包含它所属的月份。但是,我知道如果它是第一次发生,那么它会属于第 1 个月,如果它是第二次发生,那么它将属于第 2 个月。所以例如,日期 1 发生了 3 次,然后被日期中断2.所以前3次属于第1个月,接下来的3次发生,属于第2个月。所以我希望我的结果是这样的:+---------+------------------+------+------+---------+| Name    | Task             | Team | Date | Month   |+---------+------------------+------+------+---------+| John    | Market study     | A    | 1    | Month 1 |+---------+------------------+------+------+---------+| Michael | Customer service | B    | 1    | Month 1 |+---------+------------------+------+------+---------+| Joanna  | Accounting       | C    | 1    | Month 1 |+---------+------------------+------+------+---------+| John    | Accounting       | B    | 2    | Month 1 |+---------+------------------+------+------+---------+除了使用一些循环之外,我没有任何想法。谢谢你们。
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

如果我正确理解了这个问题,您可以执行以下操作:创建掩码s以将每个连续值分成单独的组。从,为每个组的每个值s创建掩码。s1Groupby s1and Dateand doing cumcountandmap创建所需的输出:


s = df.Date.ne(df.Date.shift()).cumsum()

s1 = df.Date.groupby(s).cumcount()


df['Month'] = df.groupby([s1, 'Date']).Name.cumcount().add(1).map(lambda x: 'Month '+str(x))


Out[897]:

       Name              Task Team  Date    Month

0      John      Market-study    A     1  Month 1

1   Michael  Customer-service    B     1  Month 1

2    Joanna        Accounting    C     1  Month 1

3      John        Accounting    B     2  Month 1

4   Michael  Customer-service    A     2  Month 1

5    Joanna      Market-study    C     2  Month 1

6      John  Customer-service    C     1  Month 2

7   Michael      Market-study    A     1  Month 2

8    Joanna  Customer-service    B     1  Month 2

9      John      Market-study    A     2  Month 2

10  Michael  Customer-service    B     2  Month 2

11   Joanna        Accounting    C     2  Month 2


查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号