Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为重复项添加增量值_Python_Python 3.x_Pandas - Fatal编程技术网

Python 为重复项添加增量值

Python 为重复项添加增量值,python,python-3.x,pandas,Python,Python 3.x,Pandas,假设我有一个看起来像 df = pd.DataFrame(np.array([[1, 2, 3, 2], [4, 5, 6, 3], [7, 8, 9, 5]]), columns=['a', 'b', 'c', 'repeater']) a b c repeater 0 1 2 3 2 1 4 5 6 3 2 7 8 9 5 我根据df['repeat']重复每一行,比如df=df.loc[df.index.repeat

假设我有一个看起来像

df = pd.DataFrame(np.array([[1, 2, 3, 2], [4, 5, 6, 3], [7, 8, 9, 5]]),  columns=['a', 'b', 'c', 'repeater'])

    a   b   c   repeater
0   1   2   3   2
1   4   5   6   3
2   7   8   9   5
我根据
df['repeat']
重复每一行,比如
df=df.loc[df.index.repeat(df['repeater'])]
所以我最终得到了一个数据帧

    a   b   c   repeater
0   1   2   3   2
0   1   2   3   2
1   4   5   6   3
1   4   5   6   3
1   4   5   6   3
2   7   8   9   5
2   7   8   9   5
2   7   8   9   5
2   7   8   9   5
2   7   8   9   5
如何基于索引行添加增量值?因此,一个新的列
df['incremental']
具有以下输出:

    a   b   c   repeater    incremental
0   1   2   3   2           1
0   1   2   3   2           2
1   4   5   6   3           1
1   4   5   6   3           2
1   4   5   6   3           3
2   7   8   9   5           1
2   7   8   9   5           2
2   7   8   9   5           3
2   7   8   9   5           4
2   7   8   9   5           5

使用额外的
groupby
cumcount
尝试您的代码:

df = df.loc[df.index.repeat(df['repeater'])]
df['incremental'] = df.groupby(df.index).cumcount() + 1
print(df)
输出:

   a  b  c  repeater  incremental
0  1  2  3         2            1
0  1  2  3         2            2
1  4  5  6         3            1
1  4  5  6         3            2
1  4  5  6         3            3
2  7  8  9         5            1
2  7  8  9         5            2
2  7  8  9         5            3
2  7  8  9         5            4
2  7  8  9         5            5

使用
df['incremental']=df.groupby(level=0.cumcount()