gpt4 book ai didi

python - 将项目拆分为行 Pandas

转载 作者:行者123 更新时间:2023-12-03 15:11:50 26 4
gpt4 key购买 nike

我有数据帧中的数据,如下所示。
我想将项目拆分为相同数量的行

>>> df
idx a
0 3
1 5
2 4

从上面的数据框,我想要下面的
>>> df
idx a
0 1
1 2
2 3
3 1
4 2
5 3
6 4
7 5
8 1
9 2
10 3
11 4

我尝试了几种方法,但都没有成功。

最佳答案

列表理解

pd.DataFrame({'a': [x + 1 for y in df['a'] for x in range(y)]})

a
0 1
1 2
2 3
3 1
4 2
5 3
6 4
7 5
8 1
9 2
10 3
11 4

关于python - 将项目拆分为行 Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60742389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com