Alison S
Alison S

Reputation: 1858

Update columns in Pandas Dataframe rows when criteria in the rest of the row are met (in SQL UPDATE)

The closest I've found to an answer is this: Update a dataframe in pandas while iterating row by row

However, it doesn't answer my question. Here's what I want to do:

#for each dataframe row
# if it matches criteria a, b, and c
# update two column d and e with new values

Here's a contrived dataframe example called df:

    first_name  last_name  city  state  number_of_cousins  number_of_siblings
0   Margaret    Smith      C     C      0                  0
1   April       Smith      C     D      0                  0
2   June        Smith      C     C      0                  0
3   David       Smith      A     D      0                  0

I need to get rows 0 and 2 to have 2 cousins and 3 siblings.

cousins_and_siblings = [2,3]

I know I'll need to use .iterrows() but besides that I haven't been able to find an example.

Upvotes: 2

Views: 1734

Answers (1)

b10n
b10n

Reputation: 1186

You'll not need to iterate over the rows. This can be done using vectorized methods. Create a boolean mask based on your selection criteria.

city_state_mask = (df.city == 'C') & (df.state == 'C')

Use that to select the rows and then make the assignment. You need to select the value first and then pass the mask. df["Col"][mask] = 'value' updates the original dataframe. df[mask]["Col"] = 'value' will assign to a copy which is not what you want.

df['number_of_siblings'][city_state_mask] = 2
df['number_of_cousins'][city_state_mask] = 3

Upvotes: 4

Related Questions