Prateek
Prateek

Reputation: 1072

Expand rows in python pandas dataframe

I have a dataframe as follows:

Name Description Count
a     This is a   3
b     This is b   2
c     This is c   1

I want to modify this dataframe as follows:

Name Description Count
a     This is a   1
a     This is a   1
a     This is a   1
b     This is b   1
b     This is b   1
c     This is c   1

Basically, duplicate every row by the value of a particular column.

Am new to python and not familiar with libraries yet.

Upvotes: 1

Views: 69

Answers (1)

BENY
BENY

Reputation: 323276

Try reindex with repeat

df=df.reindex(df.index.repeat(df.Count)).assign(Count=1)

Upvotes: 3

Related Questions