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

set multiple column values for multiple rows with loc

I have this Dataframe:

     A  B  C  D  E  F  G  H  I  J  K
0    .  .  .  .  .  .  X  L  .  .  .
1    .  .  .  .  .  .  X  A  .  .  .
.
.
.
300  .  .  .  .  .  .  X  R  .  .  .
301  .  .  .  .  .  . nan R  .  .  .
302  .  .  .  .  .  .  X  R  .  .  .
303  .  .  .  .  .  . nan R  .  .  .

I am trying to change columns G and I to U if H = R.
Expected output:

     A  B  C  D  E  F  G  H  I  J  K
0    .  .  .  .  .  .  X  L  .  .  .
1    .  .  .  .  .  .  X  A  .  .  .
.
.
.
300  .  .  .  .  .  .  U  R  U  .  .
301  .  .  .  .  .  .  U  R  U  .  .
302  .  .  .  .  .  .  U  R  U  .  .
303  .  .  .  .  .  .  U  R  U  .  .

Is there a way to pass multiple columns to the loc here instead of doing this twice or having to loop twice like this which looks non-pythonic:

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

df.loc[df[‘H’] == ‘R’, ‘G’] = ‘U’

df.loc[df[‘H’] == ‘R’, ‘I’] = ‘U’

I thought this would be trivial but can’t find any clear solution.

>Solution :

As @Tim already mentioned you can proceed like the following :
import pandas as pd

data = {
    'A': ['.']*304,
    'B': ['.']*304,
    'C': ['.']*304,
    'D': ['.']*304,
    'E': ['.']*304,
    'F': ['.']*304,
    'G': ['.']*301 + ['nan', 'X', 'nan'],
    'H': ['L', 'A'] + ['R']*301,
    'I': ['.']*304,
    'J': ['.']*304,
    'K': ['.']*304
}

df = pd.DataFrame(data)

# Modify 'G' and 'I' columns where 'H' is 'R'
df.loc[df['H'] == 'R', ['G', 'I']] = 'U'

print(df)

If you want to get for H=R and then sets G to U and I to M for example in a single liner :

df.loc[df['H'] == 'R', ['G', 'I']] = df.loc[df['H'] == 'R'].assign(G='U', I='M')[['G', 'I']]
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