gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:14:40 26 4
gpt4 key购买 nike

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

+---------+------------------+------+------+
| 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 |
+---------+------------------+------+------+---------+
| Michael | Customer service | A | 2 | Month 1 |
+---------+------------------+------+------+---------+
| Joanna | Market study | C | 2 | Month 1 |
+---------+------------------+------+------+---------+
| John | Customer service | C | 1 | Month 2 |
+---------+------------------+------+------+---------+
| Michael | Market study | A | 1 | Month 2 |
+---------+------------------+------+------+---------+
| Joanna | Customer service | B | 1 | Month 2 |
+---------+------------------+------+------+---------+
| John | Market study | A | 2 | Month 2 |
+---------+------------------+------+------+---------+
| Michael | Customer service | B | 2 | Month 2 |
+---------+------------------+------+------+---------+
| Joanna | Accounting | C | 2 | Month 2 |
+---------+------------------+------+------+---------+

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

最佳答案

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

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

关于python - 从日期列创建月份列(但是日期列不包含月份信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com