Claudiu Creanga
Claudiu Creanga

Reputation: 8366

Explode the rows based on integer in pandas

df = pd.DataFrame([["a", 2], ["b", 3], ["c", 1]], columns=['a', 'count'])
df
    a   count
0   a   2
1   b   3
2   c   1

this input I'd like to explode the count integer into multiple rows of 1s.

new_df = pd.DataFrame([], columns=['a', 'count'] )
def s(row):
    while row["count"] > 0:
        global new_df
        a = pd.DataFrame([[row["a"], 1]], columns=["a", "count"])
        new_df = new_df.append(a, ignore_index=True)
        row["count"] -= 1
df.apply(s, axis=1)
new_df
a   count
0   a   1
1   a   1
2   b   1
3   b   1
4   b   1
5   c   1

The way I'm doing it looks bad and inefficient. Is there a more pandorable way?

Upvotes: 3

Views: 192

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try reindex/loc on the repeated index:

(df.loc[df.index.repeat(df['count'])]
   .assign(count=1)
   .reset_index(drop=True)
)

Output:

   a  count
0  a      1
1  a      1
2  b      1
3  b      1
4  b      1
5  c      1

Upvotes: 4

Related Questions