Shawn
Shawn

Reputation: 591

Manipulating Dataframes with pandas (Python)

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.

Upvotes: 1

Views: 192

Answers (1)

jezrael
jezrael

Reputation: 863226

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

Upvotes: 2

Related Questions