将新列添加到DataFrame中,其值取决于索引ref [英] Adding new column to DataFrame with values dependent on index ref

查看:108
本文介绍了将新列添加到DataFrame中,其值取决于索引ref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Pandas的DataFrame中添加一个新列,在其中我通过索引分配一个StoreID:

I want to add a new column to this DataFrame in Pandas where I assign a StoreID rolling thru the indexes:

当前看起来像这样:

   Unnamed: 12  Store  
0          NaN      1  
1          NaN      1  
2          NaN      1  

0          NaN      1  
1          NaN      1  
2          NaN      1  

0          NaN      1  
1          NaN      1  
2          NaN      1  

0          NaN      1  
1          NaN      1  
2          NaN      1  

I希望它看起来像这样:

I want it to look like this:

   Unnamed: 12  Store  StoreID
0          NaN      1  1
1          NaN      1  1
2          NaN      1  1
0          NaN      1  2
1          NaN      1  2
2          NaN      1  2
0          NaN      1  5
1          NaN      1  5
2          NaN      1  5
0          NaN      1  11
1          NaN      1  11
2          NaN      1  11

变量在索引达到0时发生变化该报告将具有可变数量的项目-每个商店大多数记录是1000条记录中的100条。

The variable changes upon the index hitting 0. The report will have variable numbers of items - most of them being 100's of 1000s of records per store.

我可以轻松创建一个新列,但似乎无法解决该问题!
非常感谢您的帮助-我才刚开始使用Python。

I can create a new column easily but I can't seem to work out how to do this! Any help much appreciated - I'm just starting out with Python.

推荐答案

IIUC尝试 cumcount

df.groupby(df.index).cumcount()
Out[11]: 
0    0
1    0
2    0
0    1
1    1
2    1
0    2
1    2
2    2
0    3
1    3
2    3
dtype: int64

这篇关于将新列添加到DataFrame中,其值取决于索引ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆