简体   繁体   English

按增量对 pandas 列进行分组

[英]Group pandas column by increment

I have a dataframe that I would like to group based on how a column is increasing.我有一个 dataframe,我想根据列的增加方式对其进行分组。 Suppose I have this df:假设我有这个 df:

df =
col1 col2 
 0    1
 0    2
 0    3
 1    4
 0    1
 1    2
 0    1
 0    2
 1    3

And I would like to create a new column that is based on col2 's counting and hence values:我想创建一个基于col2的计数和值的新列:

result =
col1 col2 col3
 0    1   [1]
 0    2   [1,2]
 0    3   [1,2,3]
 1    4   [1,2,3,4]
 0    1   [1]
 1    2   [1,2]
 0    1   [1]
 0    2   [1,2]
 1    3   [1,2,3]

If anyone have a neat answer to this, it would be much appreciated!!如果有人对此有一个简洁的答案,将不胜感激!

I would possibly approach it like this我可能会这样处理

Create a new column containing the cumulative count创建一个包含累计计数的新列

df['col3'] = df.groupby(['col1'])['col2'].apply(lambda x: x.cumcount() + 1)

Create a new column containing the list of values创建一个包含值列表的新列

df['col3'] = df.groupby(['col1'])['col2'].apply(lambda x: list(x[:x.cumcount()+1]))

Print resulting dataframe: print(df打印结果 dataframe: print(df

    col1  col2         col3
0      0     1        [1]
1      0     2        [1, 2]
2      0     3        [1, 2, 3]
3      1     4        [1, 2, 3, 4]
4      0     1        [1]
5      1     2        [1, 2]
6      0     1        [1]
7      0     2        [1, 2]
8      1     3        [1, 2, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM