将项目拆分为大熊猫

时间:2020-03-18 15:12:21

标签: python pandas numpy dataframe

我将数据存储在如下数据框中。 我想将项目拆分为相同的行数

>>> 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  

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

4 个答案:

答案 0 :(得分:5)

一种有趣的方式

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

答案 1 :(得分:5)

列表理解

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

答案 2 :(得分:4)

这里是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'))

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

答案 3 :(得分:4)

这是一个基于numpy的游戏:

a = (np.arange(df.a.max())+1)
m = a <= df.a.values[:,None]
df = pd.DataFrame(m.cumsum(1)[m], columns=['a'])

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