根据列值熊猫的条件更新特定列的单元格值

时间:2018-08-02 07:17:48

标签: python pandas

请考虑以下df...。如果['Catalogue']=='Equity',我想将所有数据复制到列['Catalogue','Display','Shelves','Price','Mechanic'触发条件的['Week']中的]`。

在下面的示例中,['Catalogue'] =='Equity'出现在['Week'] =='1'中,因此在这种情况下,我要复制['Catalogue','中出现的数据显示”,“货架”,“价格”,“机械”] which happens to be 'Equity','Tactical,0.0,NaN,0.5 for all rows where ['Week']== '1')。

然后我想在['Price']的{​​{1}}中进行计算

样本数据集在下面

['Mechanic']*['RRP']

我想要的输出如下...

   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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用:

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