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:
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']]