Reputation: 2181
Sample Data:
X | a | c
41 | 5 | 2
54 | 3 | 1
Transformation:
X | a | c
41 | 5 | 2
41 | 5 | 2
41 | 5 | 2
41 | 5 | 2
41 | 5 | 2
41 | 5 | 2
41 | 5 | 2
54 | 3 | 1
54 | 3 | 1
54 | 3 | 1
54 | 3 | 1
54 | 3 | 1
54 | 3 | 1
54 | 3 | 1
I've done this in R using the following command:
data_final[rep(1:nrow(data_final),each=nrow(expand.grid(0:6)),]
but I'm not quite sure how I'd do this in python.
Any help would be much appreciated
Upvotes: 1
Views: 80
Reputation: 5215
This can be done by using .repeat
on your index, passing the resulting array to .reindex
, and then resetting your index with drop=True
:
df.reindex(df.index.repeat(6)).reset_index(drop=True)
# X a c
# 0 41 5 2
# 1 41 5 2
# 2 41 5 2
# 3 41 5 2
# 4 41 5 2
# 5 41 5 2
# 6 54 3 1
# 7 54 3 1
# 8 54 3 1
# 9 54 3 1
# 10 54 3 1
# 11 54 3 1
Upvotes: 0
Reputation: 887128
We can try
pd.concat([dat]*6, ignore_index = True).sort_values('X').reset_index(drop = True)
# X a c
#0 41 5 2
#1 41 5 2
#2 41 5 2
#3 41 5 2
#4 41 5 2
#5 41 5 2
#6 54 3 1
#7 54 3 1
#8 54 3 1
#9 54 3 1
#10 54 3 1
#11 54 3 1
Upvotes: 1
Reputation: 862671
pandas + numpy solution with numpy.repeat
:
If all columns have same dtypes:
df = pd.DataFrame(np.repeat(df.values, 7, axis=0), columns=df.columns)
print (df)
X a c
0 41 5 2
1 41 5 2
2 41 5 2
3 41 5 2
4 41 5 2
5 41 5 2
6 41 5 2
7 54 3 1
8 54 3 1
9 54 3 1
10 54 3 1
11 54 3 1
12 54 3 1
13 54 3 1
If not:
df = df.loc[np.repeat(df.index, 7)].reset_index(drop=True)
print (df)
X a c d
0 41 5 2 a
1 41 5 2 a
2 41 5 2 a
3 41 5 2 a
4 41 5 2 a
5 41 5 2 a
6 41 5 2 a
7 54 3 1 r
8 54 3 1 r
9 54 3 1 r
10 54 3 1 r
11 54 3 1 r
12 54 3 1 r
13 54 3 1 r
Upvotes: 3