Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replicate X times specific rows of pandas dataframe

I have a dataframe and want to replicate specific rows based on value in a column and looking for a simple way to do it.
For example in the dummy data how can i replicate row where Age is 24 – 2 more times and where Age is 22 – 2 more times.

import pandas as pd

data= [['Karan',23],['Rohit',22],['Macron',22], ['Sahil',21],['Aryan',24]]

df = pd.DataFrame(data, columns=['Name','Age'])

So my final dataframe should look like [row order not important]

Name Age
Karan 23
Rohit 22
Rohit 22
Macron 22
Macron 22
Sahil 21
Aryan 24
Aryan 24

Tried [https://stackoverflow.com/questions/24029659/python-pandas-replicate-rows-in-dataframe] but no working for data with string.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use Index.repeat:

df.loc[df.index.repeat(df['Age'].isin([22,24]).add(1))]

How it works:

  • determine whether Age is in [22,24]
  • add 1 (the False values become 1, the True become 2)
  • repeat the index and reindex

or, for more flexibility, with numpy.where, you can pick any value you want:

import numpy as np
df.loc[df.index.repeat(np.where(df['Age'].isin([22,24]), 2, 1))]

output:

     Name  Age
0   Karan   23
1   Rohit   22
1   Rohit   22
2  Macron   22
2  Macron   22
3   Sahil   21
4   Aryan   24
4   Aryan   24
resetting the index:
df.loc[df.index.repeat(df['Age'].isin([22,24]).add(1))].reset_index(drop=True)

output:

     Name  Age
0   Karan   23
1   Rohit   22
2   Rohit   22
3  Macron   22
4  Macron   22
5   Sahil   21
6   Aryan   24
7   Aryan   24
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading