简体   繁体   中英

Creating Pandas Dataframe row on the basis of other column value

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM