3

I have a dataframe which is something like this:

index buyedA total
a      2    4
b      1    2

and I need to turn it into something like this:

index buyedA total
a      1    1
a      1    1
a      0    1
a      0    1
b      1    1
b      0    1

I need for each index as many rows as specified by column total (each one filled with a value of 1), and if column buyedA says 2, I need 2 of those rows filled with a 1.

Is there a way to do so in Python?

Thanks!

2 Answers 2

3

Using repeat and a simple groupby

n = df.loc[df.index.repeat(df.total)].assign(total=1)
n['buyedA'] = n.groupby('index').total.cumsum().le(n.buyedA).astype(int)

  index  buyedA  total
0     a       1      1
0     a       1      1
0     a       0      1
0     a       0      1
1     b       1      1
1     b       0      1
Sign up to request clarification or add additional context in comments.

Comments

1

Let's try this:

#make sure index is in the dataframe index
df=df.set_index('index')

#use repeat and reindex
df_out = df.reindex(df.index.repeat(df['total'])).assign(total=1)

#Limit buyedA by row number in each group of index
df_out['buyedA'] = ((df_out.groupby('index').cumcount() + 1) <= df_out['buyedA']).mul(1)

df_out

output:

       buyedA  total
index               
a           1      1
a           1      1
a           0      1
a           0      1
b           1      1
b           0      1

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.