Let say I have a dataframe as follows:
index | col 1 | col2 |
---|---|---|
0 | a01 | a02 |
1 | a11 | a12 |
2 | a21 | a22 |
I want to duplicate a row by n times and then insert the duplicated rows at certain index. For e.g. duplicating row 0 by 2 times and then inserting before original row 0:
index | col 1 | col2 |
---|---|---|
0 | a01 | a02 |
1 | a01 | a02 |
2 | a01 | a02 |
3 | a11 | a12 |
4 | a21 | a22 |
What I'm doing now is creating an empty dataframe and then filling it with the values of the row that I want duplicated.
# create empty dataframe with 2 rows
temp_df = pd.DataFrame(columns=original_df.columns, index=list(range(2)))
# replacing each column by target value, I don't know how to do this more efficiently
temp_df.iloc[:,0] = original_df.iloc[0,0]
temp_df.iloc[:,1] = original_df.iloc[0,1]
temp_df.iloc[:,2] = original_df.iloc[0,2]
# concat the dataframes together
# to insert at indexes in the middle, I would have to slice the original_df and concat separately
original_df = pd.concat([temp_df, original_df])
This seems like a terribly obtuse way to do something I presume should be quite simple. How should I accomplish this more easily?