Python 2.7.11 //熊猫0.18.1
我有一个虚构的数据集(csv),可用于一个包含250个项目的假想酒类商店。这些列涵盖“啤酒”,“标签”,“年份”,“商店价格”,“ MSRP”,“供应商价格”等。但是,对于此问题,相关的部分是啤酒厂和商店价格(在结帐时查询的当前价格)。
Brewery Store Price
104 Glenfiddich 109.99
105 Glenfiddich 89.99
108 Glenfiddich 114.99
110 Glenfiddich 99.99
119 Glenfiddich 169.99
如果我要在Glenfiddich上进行销售,则可以通过以下方式找到Glenfiddich的商品:
df = pd.read_csv('liquorStore.csv')
df.Brewery.str.contains('Glenfiddich')
我知道如何找到 Glenfiddich 产品,但我不知道如何更改数据框中行的值。例如,我要:
注意:我只是在做熊猫练习。
您可以使用loc
with boolean indexing
for select ,然后使用 multiple by 0.9
:
df.loc[df.Brewery == 'Glenfiddich', 'Store Price'] *= .9
样本:
print (df)
Brewery Store Price
104 Glenfiddich 109.99
105 Glenfiddich 89.99
120 Another 100.00
df.loc[df.Brewery == 'Glenfiddich', 'Store Price'] *= .9
print (df)
Brewery Store Price
104 Glenfiddich 98.991
105 Glenfiddich 80.991
120 Another 100.000
另一种可能的解决方案是使用mask
:
df['Store Price'] = df['Store Price'].mask(df.Brewery == 'Glenfiddich',
df['Store Price'] * .9)
print (df)
Brewery Store Price
104 Glenfiddich 98.991
105 Glenfiddich 80.991
120 Another 100.000
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句