How can insert rows in a pandas dataframe? I have a large dataframe and I am trying to identify specific values to repeat rows and inserting in the dataframe. For example:
df1 = pd.DataFrame([[1, 2], [3, 4],[1, 6],[2,3],[1,5]], columns=["a", "b"])
a b
0 1 2
1 3 4
2 1 6
3 2 3
4 1 5
Column "a" represent the number of row repetitions to inset in the dataframe, So I would like to get the following
a b
0 1 2
1 1 4
2 1 4
3 1 4
4 1 6
5 1 3
6 1 3
7 1 5
I tried to use append but the results is not what I expected. Here is what I have so far. I'll appreciate any insight.
df2 = df1[df1.a > 1] # To select rows with values more than 1
repeats = (df2.iloc[0]["a"] - 1) # number of repetitions -1
r2 = pd.concat([df2]*repeats, ignore_index=True)
df_modified = df1.append(r2, ignore_index=True)