i've got a dataframe with three columns. Each row needs to be copied and altered twice based on the values in that specific row and column. However, the values in the other columns need to stay the same.
I've managed to make the dataframe, as follows:
df = pd.DataFrame({'Value': list(range(3)), 'Value2': list(range(3)), 'Value3':['A','B','C']})
idx = df['Value'].index
# construct dataframe to append
df_extra1 = df.loc[idx].copy()
df_extra2 = df.loc[idx].copy()
df_extra3 = df.loc[idx].copy()
df_extra4 = df.loc[idx].copy()
# add 3 seconds
df_extra1['Value'] = df_extra1['Value'] + 0.1
df_extra2['Value'] = df_extra2['Value'] - 0.1
df_extra3['Value2'] = df_extra3['Value2'] + 0.1
df_extra4['Value2'] = df_extra4['Value2'] - 0.1
# append to original
res1 = df.append(df_extra1)
res2 = res1.append(df_extra2)
res3 = res2.append(df_extra3)
res4 = res3.append(df_extra4)
This is what the result is and should look like:
Value Value2 Value3
0 0.0 0.0 A
1 1.0 1.0 B
2 2.0 2.0 C
0 0.1 0.0 A
1 1.1 1.0 B
2 2.1 2.0 C
0 -0.1 0.0 A
1 0.9 1.0 B
2 1.9 2.0 C
0 0.0 0.1 A
1 1.0 1.1 B
2 2.0 2.1 C
0 0.0 -0.1 A
1 1.0 0.9 B
2 2.0 1.9 C
Is there anyway to speed this up or make it more concise?