Replicating rows in Pandas [duplicate]

  • A+
Category:Languages

This question already has an answer here:

My pandas dataframe looks like this:

   Person  ID   ZipCode   Gender 0  12345   882  38182     Female 1  32917   271  88172     Male 2  18273   552  90291     Female 

I want to replicate every row 3 times like:

   Person  ID   ZipCode   Gender 0  12345   882  38182     Female 0  12345   882  38182     Female 0  12345   882  38182     Female 1  32917   271  88172     Male 1  32917   271  88172     Male 1  32917   271  88172     Male 2  18273   552  90291     Female 2  18273   552  90291     Female 2  18273   552  90291     Female 

And of course reset the index so it is:

0 1 2 

I tried solutions such as:

pd.concat([df[:5]]*3, ignore_index=True) 

and:

df.reindex(np.repeat(df.index.values, df['ID']), method='ffill') 

I have had no luck, if you can help I'd appreciate it.

 


Try this:

newdf = pd.DataFrame(np.repeat(df.values,3,axis=0)) newdf.columns = df.columns print(newdf) 

Output:

  Person   ID ZipCode  Gender 0  12345  882   38182  Female 1  12345  882   38182  Female 2  12345  882   38182  Female 3  32917  271   88172    Male 4  32917  271   88172    Male 5  32917  271   88172    Male 6  18273  552   90291  Female 7  18273  552   90291  Female 8  18273  552   90291  Female 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: