Reputation: 191
I have a dataframe having three column :
order_no product quantity
0 5bf69f 3
0 5beaba 2
1 5bwq21 1
1 5bf69f 1
I want to create row if quantity value is greater than 1 like this:
order_no product quantity
0 5bf69f 1
0 5bf69f 1
0 5bf69f 1
0 5beaba 1
0 5beaba 1
1 5bwq21 1
1 5bf69f 1
Upvotes: 3
Views: 406
Reputation: 862511
First is necessary unique index values, so if necessary:
df = df.reset_index(drop=True)
Then use Index.repeat
by column quantity
and expand rows by DataFrame.loc
, set column to 1
by DataFrame.assign
and last again create unique index by DataFrame.reset_index
:
df = df.loc[df.index.repeat(df['quantity'])].assign(quantity=1).reset_index(drop=True)
print (df)
order_no product quantity
0 0 5bf69f 1
1 0 5bf69f 1
2 0 5bf69f 1
3 0 5beaba 1
4 0 5beaba 1
5 1 5bwq21 1
6 1 5bf69f 1
Using numpy.repeat
is possible, but numpy cast all data to object, because string
column:
print (pd.DataFrame(np.repeat(df.values,df.quantity,axis=0)).dtypes)
0 object
1 object
2 object
dtype: object
Upvotes: 3