假设我有以下数据帧:

date          group         value
2022-11-01.     1              4
2022-11-02.     1              12
2022-11-03.     1              14
2022-11-04.     1              25
2021-11-01.     2              9
2021-11-02.     2              7
2019-10-01.     3              40
2022-10-02.     3              14

我想 for each 组创建一个基于日期递增整数的新列.例如,这是所需的输出:

  date          group         value      new_col
    2022-11-01.     1              4.      0
    2022-11-02.     1              12.     1
    2022-11-03.     1              14.     2
    2022-11-04.     1              25.     3
    2021-11-01.     2              9.      0
    2021-11-02.     2              7.      1
    2019-10-01.     3              40.     0
    2022-10-02.     3              14.     1

您看,new_col1大概是np,arange(0, len(df['date'])+1)个,但是我想按组来做,而且似乎没有任何Groupby的变体适合我.

我试过了:

df.groupby('group')['date'].apply(lambda x: np.arange(0, len(x)+1)

然而,这与我想要的还差得很远.如果有人能解释如何正确地做这件事,我将不胜感激.

推荐答案

有没有其他方法可以使用np.arange(0,len(X)+1)和groupby来解决这个问题?

我更改了See Difference-GroupBy.rank的数据,使用列date的顺序,因此不同的输出使用计数器GroupBy.cumcount和您的解决方案GroupBy.transform:

print (df)
        date  group  value
0 2022-11-08      1      4
1 2022-11-07      1     12
2 2022-11-03      1     14
3 2022-11-04      1     25
4 2021-11-21      2      9
5 2021-11-02      2      7
6 2019-10-01      3     40
7 2022-10-02      3     14

df['new_col'] = df.groupby('group')['date'].rank('dense').sub(1).astype(int)

df['new_col1'] = df.groupby('group').cumcount()

df['new_col2'] = df.groupby('group')['date'].transform(lambda x: np.arange(len(x)))
print (df)
        date  group  value  new_col  new_col1  new_col2
0 2022-11-08      1      4        3         0         0
1 2022-11-07      1     12        2         1         1
2 2022-11-03      1     14        0         2         2
3 2022-11-04      1     25        1         3         3
4 2021-11-21      2      9        1         0         0
5 2021-11-02      2      7        0         1         1
6 2019-10-01      3     40        0         0         0
7 2022-10-02      3     14        1         1         1

如果希望相同的输出解决方案按两列排序:

df = df.sort_values(['group','date'])

df['new_col'] = df.groupby('group')['date'].rank('dense').sub(1).astype(int)

df['new_col1'] = df.groupby('group').cumcount()

df['new_col2'] = df.groupby('group')['date'].transform(lambda x: np.arange(len(x)))
print (df)
        date  group  value  new_col  new_col1  new_col2
2 2022-11-03      1     14        0         0         0
3 2022-11-04      1     25        1         1         1
1 2022-11-07      1     12        2         2         2
0 2022-11-08      1      4        3         3         3
5 2021-11-02      2      7        0         0         0
4 2021-11-21      2      9        1         1         1
6 2019-10-01      3     40        0         0         0
7 2022-10-02      3     14        1         1         1

Python相关问答推荐

为什么图像结果翻转了90度?

当变量也可以是无或真时,判断是否为假

在Python中,如何才能/应该使用decorator 来实现函数多态性?

根据多列和一些条件创建新列

Snap 7- read_Area用于类似地址的变量

为什么我的(工作)代码(生成交互式情节)在将其放入函数中时不再工作?

pyautogui.locateOnScreen在Linux上的工作方式有所不同

如何在Python中使用时区夏令时获取任何给定本地时间的纪元值?

如何根据条件在多指标框架上进行groupby

Pythind 11无法弄清楚如何访问tuple元素

DataFrame groupby函数从列返回数组而不是值

如何让剧作家等待Python中出现特定cookie(然后返回它)?

为什么这个带有List输入的简单numba函数这么慢

Python中绕y轴曲线的旋转

如何在Python脚本中附加一个Google tab(已经打开)

运输问题分支定界法&

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

Plotly Dash Creating Interactive Graph下拉列表

判断solve_ivp中的事件

在pandas数据框中计算相对体积比指标,并添加指标值作为新列