首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在熊猫群之后,每隔两行指定一组的索引号

在熊猫群之后,每隔两行指定一组的索引号
EN

Stack Overflow用户
提问于 2022-11-22 11:19:19
回答 2查看 29关注 0票数 0

我有如下所示的数据:

代码语言:javascript
复制
 TileDesc       ReportDesc     UrlLink     
   'AA'       'New Report-1'   'link-1'
   'AA'       'New Report-2'   'link-2'
   'AA'       'New Report-1'   'link-1'
   'AA'       'New Report-1'   'link-1'
   'AA'       'New Report-1'   'link-1'
   'BB'       'New Report-4'   'link-4'
   'BB'       'New Report-2'   'link-2'
   'BB'       'New Report-4'   'link-4'
   'BB'       'New Report-6'   'link-6'

现在,我想添加一个列,它将保持一个整数序列,在连续两次之后会改变一次。因此,生成的dataframe如下所示:

代码语言:javascript
复制
 TileDesc       ReportDesc     UrlLink    Group     
   'AA'       'New Report-1'   'link-1'     1
   'AA'       'New Report-2'   'link-2'     1
   'AA'       'New Report-1'   'link-1'     2
   'AA'       'New Report-4'   'link-4'     2
   'AA'       'New Report-6'   'link-1'     3
   'BB'       'New Report-4'   'link-4'     1
   'BB'       'New Report-2'   'link-2'     1
   'BB'       'New Report-4'   'link-4'     2
   'BB'       'New Report-6'   'link-6'     2

我正在遵循ngroup()的方法,但无法通过。

代码语言:javascript
复制
df['Group'] = df.groupby(['TileDesc']).ngroup()

上面的代码片段为每个组提供了相同的组号。也就是说,对于所有三个AA,我得到0,然后对于所有BB,得到1等等。

我的第二种方法更像是:

代码语言:javascript
复制
df['Index'] = df.index + 1
df['Group'] = df['Index'].apply(lambda x : math.ceil(x/4))

但这不考虑TileDesc

我在这里错过了什么?

编辑组的值只有在TileDesc组中的每两个连续行之后才会更改。

EN

回答 2

Stack Overflow用户

发布于 2022-11-22 11:32:36

IIUC,您可以使用cumcount()进行分组。增加的诀窍是,您可以将初始的0( cumcount从0开始)替换为空,并替换为1(即bfill):

代码语言:javascript
复制
df['Group'] =  df.groupby('TileDesc').cumcount().replace(0,np.nan).bfill().astype(int)

结果:

代码语言:javascript
复制
   TileDesc      ReportDesc   UrlLink  Group
0      'AA'  'New Report-1'  'link-1'      1
1      'AA'  'New Report-2'  'link-2'      1
2      'AA'  'New Report-1'  'link-1'      2
3      'BB'  'New Report-4'  'link-4'      1
4      'BB'  'New Report-2'  'link-2'      1
5      'BB'  'New Report-4'  'link-4'      2
6      'CC'  'New Report-4'  'link-4'      1
7      'CC'  'New Report-2'  'link-2'      1
8      'CC'  'New Report-4'  'link-4'      2
9      'CC'  'New Report-4'  'link-4'      3
10     'CC'  'New Report-2'  'link-2'      4
11     'CC'  'New Report-4'  'link-4'      5

增加了一个额外的'CC‘部分来演示。

票数 0
EN

Stack Overflow用户

发布于 2022-11-22 12:31:25

使用累积和,但//2 +1只增加第二行

(对不起,我的复制粘贴有点坏了,但它起作用了)

代码语言:javascript
复制
In [38]: df
Out[38]:
  TileDesc ReportDesc   UrlLink
0     'AA'  Report-1'  'link-1'
1     'AA'  Report-2'  'link-2'
2     'AA'  Report-1'  'link-1'
3     'AA'  Report-1'  'link-1'
4     'AA'  Report-1'  'link-1'
5     'BB'  Report-4'  'link-4'
6     'BB'  Report-2'  'link-2'
7     'BB'  Report-4'  'link-4'
8     'BB'  Report-6'  'link-6'

In [39]: df['Group'] = df.groupby('TileDesc').cumcount() // 2 + 1

In [40]: df
Out[40]:
  TileDesc ReportDesc   UrlLink  Group
0     'AA'  Report-1'  'link-1'      1
1     'AA'  Report-2'  'link-2'      1
2     'AA'  Report-1'  'link-1'      2
3     'AA'  Report-1'  'link-1'      2
4     'AA'  Report-1'  'link-1'      3
5     'BB'  Report-4'  'link-4'      1
6     'BB'  Report-2'  'link-2'      1
7     'BB'  Report-4'  'link-4'      2
8     'BB'  Report-6'  'link-6'      2
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74531796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档