1

Python 2.7.11 // Pandas 0.18.1

I have a made up dataset (csv) to practice with for an imaginary liquor store with 250 items. The columns cover 'Brewery', 'Label', 'Year', 'Store Price', 'MSRP', 'Supplier Price', and a few others. However for this question, the relevant pieces are the Brewery and the Store Price (current price that is queried at checkout).

         Brewery  Store Price
104  Glenfiddich       109.99
105  Glenfiddich        89.99
108  Glenfiddich       114.99
110  Glenfiddich        99.99
119  Glenfiddich       169.99

If I were running a sale on Glenfiddich, I could locate the Glenfiddich items with something like this:

df = pd.read_csv('liquorStore.csv')    
df.Brewery.str.contains('Glenfiddich')

I know how to find the Glenfiddich products but I do not know how to alter values of the row within the dataframe. For instance, I want to:

  1. Find 'Glenfiddich' items
  2. Adjust 'Store Price' to reflect a sale/new price (e.g. 10% off)

Note: I am just doing this to practice with pandas.

1 Answer 1

2

You can use loc with boolean indexing for select and then multiple by 0.9:

df.loc[df.Brewery == 'Glenfiddich', 'Store Price'] *= .9

Sample:

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

Another possible solution is use 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
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, It is another solution, but if check here The rough rule is any time you see back-to-back square brackets, ][, you're in asking for trouble. Replace that with a .loc[..., ...] and you'll be set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.