Reputation: 194
I have the following dataframe df
:
id | mod | freq |
---|---|---|
ab01 | car | 1 |
ab01 | car | 2 |
ab02 | cycle | 1 |
ab03 | bus | 1 |
Here if the mod
is 'car', then I have to repeat those rows for each id with mod
changed to 'van'.
My output should look like this for the above data:
id | mod | freq |
---|---|---|
ab01 | car | 1 |
ab01 | car | 2 |
ab01 | van | 1 |
ab01 | van | 2 |
ab02 | cycle | 1 |
ab03 | bus | 1 |
How to get this result using pandas?
Upvotes: 1
Views: 267
Reputation: 28322
You can obtain the rows you want to duplicate, change the mod
column to be "van" and then use concat
to concatenate the new dataframe with the original one:
pd.concat([df, df.loc[df['mod'] == 'car'].assign(mod='van')]).sort_values(['id', 'mod'])
Result:
id mod freq
0 ab01 car 1
1 ab01 car 2
0 ab01 van 1
1 ab01 van 2
2 ab02 cycle 1
3 ab03 bus 1
Upvotes: 1