Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Avoiding iteration in pandas when I want to update the value in a column x when a condition is true where x is given by another column

I have the following pandas dataframe:

key1 key2 col_name bool col_1 col_2 col_3
a1 a2 col_1 0 5 10 20
b1 b2 col_3 1 10 10 5
c1 c2 col_1 1 5 15 5

Where bool==1, I would like to update the value in the column given by the col_name column to be 100.

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

key1 key2 col_name bool col_1 col_2 col_3
a1 a2 col_1 0 5 10 20
b1 b2 col_3 1 10 10 100
c1 c2 col_1 1 100 15 5

I can do this by iterating through the table, but from what I’ve read this is never best practice. What would be the most efficient way of doing this?

Cheers!

>Solution :

Build a boolean mask with and update:

# identify cells for which the col_name matches the column name
# only keep those that have a bool of 1 in the row
m = ((df['col_name'].to_numpy()[:, None] == df.columns.to_numpy())
     & df['bool'].eq(1).to_numpy()[:, None]
    )

df[m] = 100

Output:

  key1 key2 col_name  bool  col_1  col_2  col_3
0   a1   a2    col_1     0      5     10     20
1   b1   b2    col_3     1     10     10    100
2   c1   c2    col_1     1    100     15      5

Intermediate m:

array([[False, False, False, False, False, False, False],
       [False, False, False, False, False, False,  True],
       [False, False, False, False,  True, False, False]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading