I have a pandas.DataFrame
with Stockcode and quantity:
>>>df
StockCode Quantity
0 85123A 6
1 71053 6
2 84406B 8
3 84029G 6
4 84029E 6
I'm looking for a solution to get the pandas.get_dummies()
multiplied by the quantity.
The output I'm expecting should look like that:
>>>pd.get_dummies(df['StockCode']) ... --> * df['Quantity']
71053 84029E 84029G 84406B 85123A
0 0 0 0 0 6
1 6 0 0 0 0
2 0 0 0 8 0
3 0 0 6 0 0
4 0 6 0 0 0
I can do a for loop to multiply all the dummies by the quantity, but I'm hoping a more "pythonic" solution.
Does anyone know if there is better way to get those corrected dummies ?
Thanks
Larry
df.reset_index().pivot('index','StockCode','Quantity').fillna(0)
Out[93]:
StockCode 71053 84029E 84029G 84406B 85123A
index
0 0.0 0.0 0.0 0.0 6.0
1 6.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 8.0 0.0
3 0.0 0.0 6.0 0.0 0.0
4 0.0 6.0 0.0 0.0 0.0
To fix your code
pd.get_dummies(df['StockCode']).mul(df.Quantity,0)
Out[97]:
71053 84029E 84029G 84406B 85123A
0 0 0 0 0 6
1 6 0 0 0 0
2 0 0 0 8 0
3 0 0 6 0 0
4 0 6 0 0 0
Or pd.get_dummies(df['StockCode']).values*df.Quantity.values[:,None]
This also works
df.StockCode.str.get_dummies().mul(df.Quantity, axis=0)
Reference
How to multiply each row in pandas dataframe by a different value
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.get_dummies.html
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.