3
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?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.