Consider the following df.... In the event where ['Catalogue']=='Equity'
, I would like to copy all data in columns ['Catalogue','Display','Shelves','Price','Mechanic']` in the ['Week'] for which the condition is triggered.
In the example below,['Catalogue']=='Equity' occurs in ['Week']== '1' so in this situation, I would like to copy data that occurs in ['Catalogue','Display','Shelves','Price','Mechanic'] which happens to be 'Equity','Tactical,0.0,NaN,0.5 for all rows where ['Week']== '1'
).
I would then like to do a calculation in ['Price']
of ['Mechanic']*['RRP']
Sample Dataset is below
Product Name Year Customer Week RRP Catalogue Display Shelves Price Mechanic
0 product1 2016 Customer1 1 6.99 EQUITY Tactical 0.0 NaN 0.5
1 product2 2016 Customer1 1 3.49 NaN NaN NaN NaN NaN
2 product3 2016 Customer1 1 3.49 NaN NaN NaN NaN NaN
3 product1 2016 Customer1 2 6.99 NaN NaN NaN NaN NaN
4 product2 2016 Customer1 2 3.49 NaN NaN NaN NaN NaN
5 product3 2016 Customer1 2 3.49 NaN NaN NaN NaN NaN
6 product1 2016 Customer1 3 6.99 NaN Shelf NaN 2.44 3 for 2
7 product2 2016 Customer1 3 3.49 NaN Shelf NaN 3.28 3 for 2
8 product3 2016 Customer1 3 3.49 NaN Shelf NaN 1.97 3 for 2
9 product1 2016 Customer1 4 6.99 NaN Shelf NaN 2.44 3 for 2
10 product2 2016 Customer1 4 3.49 NaN Shelf NaN 3.28 3 for 2
11 product3 2016 Customer1 4 3.49 NaN Shelf NaN 1.97 3 for 2
12 product1 2016 Customer1 5 6.99 NaN NaN NaN NaN NaN
13 product2 2016 Customer1 5 3.49 NaN NaN NaN NaN NaN
14 product3 2016 Customer1 5 3.49 NaN NaN NaN NaN NaN
My desired output is below...
Product Name Year Customer Week RRP Catalogue Display Shelves Price Mechanic
0 product1 2016 Customer1 1 6.99 EQUITY Tactical 0.0 3.50 0.5
1 product2 2016 Customer1 1 3.49 EQUITY Tactical 0.0 1.74 0.5
2 product3 2016 Customer1 1 3.49 EQUITY Tactical 0.0 1.74 0.5
3 product1 2016 Customer1 2 6.99 NaN NaN NaN NaN NaN
4 product2 2016 Customer1 2 3.49 NaN NaN NaN NaN NaN
5 product3 2016 Customer1 2 3.49 NaN NaN NaN NaN NaN
6 product1 2016 Customer1 3 6.99 NaN Shelf NaN 2.44 3 for 2
7 product2 2016 Customer1 3 3.49 NaN Shelf NaN 3.28 3 for 2
8 product3 2016 Customer1 3 3.49 NaN Shelf NaN 1.97 3 for 2
9 product1 2016 Customer1 4 6.99 NaN Shelf NaN 2.44 3 for 2
10 product2 2016 Customer1 4 3.49 NaN Shelf NaN 3.28 3 for 2
11 product3 2016 Customer1 4 3.49 NaN Shelf NaN 1.97 3 for 2
12 product1 2016 Customer1 5 6.99 NaN NaN NaN NaN NaN
13 product2 2016 Customer1 5 3.49 NaN NaN NaN NaN NaN
14 product3 2016 Customer1 5 3.49 NaN NaN NaN NaN NaN
Can anyone help?
Use:
cols = ['Catalogue','Display','Shelves','Price','Mechanic']
m1 = df['Catalogue']=='EQUITY'
#boolean mask for all Weeks contains at least one EQUITY in column Catalogue
m2 = df['Week'].isin(df.loc[m1, 'Week'])
#filter df and for week forward and then back filling missing values
df[m2] = df[m2].groupby('Week').ffill().groupby('Week').bfill()
#convert column to numeric
mech = pd.to_numeric(df.loc[m2, 'Mechanic'], errors='coerce')
#multiple filtered columns
df.loc[m2, 'Price'] = (df.loc[m2, 'RRP'] * mech).round(2)
#for temporary display for disable representation of dataframes to stretch across pages
with pd.option_context('display.expand_frame_repr', False):
print (df)
Product Name Year Customer Week RRP Catalogue Display Shelves Price Mechanic
0 product1 2016 Customer1 1 6.99 EQUITY Tactical 0.0 3.50 0.5
1 product2 2016 Customer1 1 3.49 EQUITY Tactical 0.0 1.74 0.5
2 product3 2016 Customer1 1 3.49 EQUITY Tactical 0.0 1.74 0.5
3 product1 2016 Customer1 2 6.99 NaN NaN NaN NaN NaN
4 product2 2016 Customer1 2 3.49 NaN NaN NaN NaN NaN
5 product3 2016 Customer1 2 3.49 NaN NaN NaN NaN NaN
6 product1 2016 Customer1 3 6.99 NaN Shelf NaN 2.44 3 for 2
7 product2 2016 Customer1 3 3.49 NaN Shelf NaN 3.28 3 for 2
8 product3 2016 Customer1 3 3.49 NaN Shelf NaN 1.97 3 for 2
9 product1 2016 Customer1 4 6.99 NaN Shelf NaN 2.44 3 for 2
10 product2 2016 Customer1 4 3.49 NaN Shelf NaN 3.28 3 for 2
11 product3 2016 Customer1 4 3.49 NaN Shelf NaN 1.97 3 for 2
12 product1 2016 Customer1 5 6.99 NaN NaN NaN NaN NaN
13 product2 2016 Customer1 5 3.49 NaN NaN NaN NaN NaN
14 product3 2016 Customer1 5 3.49 NaN NaN NaN NaN NaN
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.