A Python Beginner’s Look at .loc. As a Python beginner, using .loc to... | by Joseph H | Towards Data Science

As a Python beginner, using .loc to retrieve and update values in a pandas dataframe just wasn’t clicking for me. In an earlier post, I shared what I’d learned about retrieving data with .loc. Today, we’ll talk about setting values.
As a refresher, here are the first 10 rows of the Pokémon data we’re working with:
Setting data values is easy with .loc, so let’s set the values for Mega Venusaur to 0. Just pass the index value for the row Mega Venusaur appears in (i.e. 3):
df.loc[3] = 0df.head(5)
Now, let’s reset the “Speed” stat for all Pokémon to 60 by passing all rows (with the :) and the column name:
df.loc[:, 'Speed'] = 60df.head(5)
Finally, let’s update the hit points for all the “PsychicFairy” type Pokémon to 42. (If you need a refresher on how to build the Boolean mask to do this, see this post.)
df.loc[df['Type'] == 'PsychicFairy', ['HP']] = 42
With this method, every cell returned by the .loc expression will be updated with the value you specify, and they’ll be changed directly in the dataframe, so it’s a good idea to first test that you’re getting the rows/columns you expect without setting the value. When I was building this example, for instance, I accidentally had ['Name', 'HP'] in the column indexer, so I ended up with a bunch of PsychicFairies named ‘42’ with 42 hit points, and I had to re-load my data.
But what if you need to set the values for a group of cells to different values for each cell? All you need is a list of the values you want to set (equal in shape to the data returned by your .loc statement), and .loc will do it for you.
As an example, let’s say we wanted to change the names of the Fire type Pokémon to include the 🔥 emoji:
Build a Boolean mask for the Fire type:df[‘Type’] == ‘Fire’Pass the mask to the DataFrame to filter for rows with Fire type Pokémon: df[df[‘Type’] == ‘Fire’]Isolate the Name column by slicing the DataFrame:df[df[‘Type’] == ‘Fire’][‘Name’]).Next, use the mask to build a list comprehension to generate the new emoji-enhanced Names. We’ll assign this to a variable called new_names: new_names = [‘🔥’ + name + ‘🔥’ for name in df[df[‘Type’] == ‘Fire’][‘Name’]].Finally, use the same Boolean mask from Step 1 and the Name column as the indexers in a.loc statement, and set it equal to the list of fiery Names:
Build a Boolean mask for the Fire type:df[‘Type’] == ‘Fire’
Pass the mask to the DataFrame to filter for rows with Fire type Pokémon: df[df[‘Type’] == ‘Fire’]
Isolate the Name column by slicing the DataFrame:df[df[‘Type’] == ‘Fire’][‘Name’]).
Next, use the mask to build a list comprehension to generate the new emoji-enhanced Names. We’ll assign this to a variable called new_names: new_names = [‘🔥’ + name + ‘🔥’ for name in df[df[‘Type’] == ‘Fire’][‘Name’]].
Finally, use the same Boolean mask from Step 1 and the Name column as the indexers in a.loc statement, and set it equal to the list of fiery Names:
df.loc[df['Type'] == 'Fire', 'Name'] = new_names
Updates to multiple columns are easy, too. And .loc will set values in any number of rows & columns you like; just pass values of the same dimensions.
Let’s change the Attack and Special Attack values for some of the Psychic Fairy types.
Here we’ll have .loc reference three rows and two columns, along with a list of values of the same dimensions, and we’re good to go.
rows_to_update = [305, 306, 487]cols_to_update = ['Attack', 'SpecialAttack']values = [[87,88],[187,188],[287,288]]df.loc[rows_to_update, cols_to_update] = values
So that’s what I learned about .loc. What questions do you have? Other tips?