ML8*_*L85 5 python numpy dataframe pandas
我有数据帧中的数据,如下所示。我想将项目拆分为相同数量的行
>>> df
idx a
0 3
1 5
2 4
Run Code Online (Sandbox Code Playgroud)
从上面的数据框,我想要下面的
>>> 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
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,但都没有成功。
这是一种series.repeat+Groupby. cumcount 假设idx是索引的方法 - 如果不是df.set_index('idx')['a']..rest of the code..
(df['a'].repeat(df['a']).groupby(level=0).cumcount().add(1)
.reset_index(drop=True).rename_axis('idx'))
Run Code Online (Sandbox Code Playgroud)
idx
0 1
1 2
2 3
3 1
4 2
5 3
6 4
7 5
8 1
9 2
10 3
11 4
dtype: int64
Run Code Online (Sandbox Code Playgroud)
有趣的方式
df.a.map(range).explode()+1 # may add reset_index(), however, I think keep the original index is good, and help us convert back.
Out[158]:
idx
0 1
0 2
0 3
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
Name: a, dtype: object
Run Code Online (Sandbox Code Playgroud)
这是一个基于 numpy 的:
a = (np.arange(df.a.max())+1)
m = a <= df.a.values[:,None]
df = pd.DataFrame(m.cumsum(1)[m], columns=['a'])
Run Code Online (Sandbox Code Playgroud)
print(df)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |