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

我有这样的数据,并想创建一个名为“月”的列


+---------+------------------+------+------+

| 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 |

+---------+------------------+------+------+---------+


除了使用一些循环之外,我没有任何想法。谢谢你们。


慕工程0101907
浏览 156回答 1
1回答

一只甜甜圈

如果我正确理解了这个问题,您可以执行以下操作:创建掩码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    Month0      John      Market-study    A     1  Month 11   Michael  Customer-service    B     1  Month 12    Joanna        Accounting    C     1  Month 13      John        Accounting    B     2  Month 14   Michael  Customer-service    A     2  Month 15    Joanna      Market-study    C     2  Month 16      John  Customer-service    C     1  Month 27   Michael      Market-study    A     1  Month 28    Joanna  Customer-service    B     1  Month 29      John      Market-study    A     2  Month 210  Michael  Customer-service    B     2  Month 211   Joanna        Accounting    C     2  Month 2
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python