Beta
Beta

Reputation: 1746

Replicating a Row in Pandas Dataframe

I've a dataset in this format:

df = pd.DataFrame({'ID': [1,2,3,4],
                   'Type': ['A', 'B', 'C', 'D'],
                   'Value': [100, 200, 201, 120]})

I want to replicate one of this dataframe more than once and add new ID which is incremental. So, the new dataset looks like the following:

df = pd.DataFrame({'ID': [1,2,3,4, 10, 11, 12, 13],
                       'Type': ['A', 'B', 'C', 'D', 'B', 'B', 'B', 'B'],
                       'Value': [100, 200, 201, 120, 200, 200, 200, 200]})

I could replicate the rows in the following way:

df_B = df[df['ID'] == 'B']
df_B = pd.concat([df_B]*4)

But I've trouble updating the ID column incrementally.

If someone can guide me.

Thanks!

Upvotes: 2

Views: 60

Answers (2)

jezrael
jezrael

Reputation: 862641

You can specify starting value for incremental new column and add it by DataFrame.assign, also add original by DataFrame.append:

N = 4
first = 10

df_B = df[df['Type'] == 'B']
df_B = df.append(pd.concat([df_B]*N).assign(ID=range(first, first + N)), ignore_index=True)
print (df_B)
   ID Type  Value
0   1    A    100
1   2    B    200
2   3    C    201
3   4    D    120
4  10    B    200
5  11    B    200
6  12    B    200
7  13    B    200

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

Do you mean by something like?

print(df.append(df.loc[df['Type'].eq('B')].reset_index(drop=True).iloc[[0] * 4].reset_index(drop=True).assign(ID=df['ID'] + 9), ignore_index=True))

Output:

   ID Type  Value
0   1    A    100
1   2    B    200
2   3    C    201
3   4    D    120
4  10    B    200
5  11    B    200
6  12    B    200
7  13    B    200

Upvotes: 2

Related Questions