Home > Blockchain >  Duplicate/multiply rows in Dataframe if contain a certain condition/string
Duplicate/multiply rows in Dataframe if contain a certain condition/string

Time:10-25

Please see the following dataframe.

import pandas as pd

data = [['PMO', 10], ['LOL', 15], ['juli', 14], ['PMO', 10], ['LOL',
15], ['juli', 14]]
df = pd.DataFrame(data, columns=['Name', 'Age'])

print(df)

I would like to iterate through this illustrative dataframe and copy/multiply the rows so that if a row contains, or matches PMO, that the row is duplicated underneath. Would this be possible, if so how?

Kind regards

CodePudding user response:

Here is a small trick, check if Name equals PMO to get a boolean Series and add 1 to transform to an integer Series of 1/2. Use this to repeat the index:

out = df.loc[df.index.repeat(df['Name'].eq('PMO').add(1))]

For a more classical way, use concat:

out = pd.concat([df, df.loc[df['Name'].eq('PMO')]]).sort_index()

output:

   Name  Age
0   PMO   10
0   PMO   10
1   LOL   15
2  juli   14

replicate rows with 'juli' 6 times:

N = 6
out = df.loc[df.index.repeat(df['Name'].eq('juli').mul(N-1).add(1))]

output (with only N=3 to keep it short):

   Name  Age
0   PMO   10
1   LOL   15
2  juli   14
2  juli   14
2  juli   14
3   PMO   10
4   LOL   15
5  juli   14
5  juli   14
5  juli   14
  • Related