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

pandas problem when assigning value using loc

So what is happening is the values in column B are becoming NaN. How would I fix this so that it does not override other values?

import pandas as pd
import numpy as np
# %%
# df=pd.read_csv('testing/example.csv')
data = {
    'Name' : ['Abby', 'Bob', 'Chris'],
    'Active' : ['Y', 'Y', 'N'],
    'A' : [89, 92, np.nan],
    'B' : ['eye', 'hand', np.nan],
    'C' : ['right', 'left', 'right'] 
}

df = pd.DataFrame(data)
df.loc[((df['Active'] =='N') & (df['A'].isna())), ['A', 'B']] = [99, df['C']]
df 

What I want the results to be is:

Name Active A B C
Abby Y 89.0 eye right
Bob Y 92.0 hand left
Chris N 99 right right

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

>Solution :

In the line where you assign the new values, you need to use the apply function to replace the values in column ‘B’ with the corresponding values from column ‘C’. Following is the modified code:

import pandas as pd
import numpy as np

data = {
    'Name' : ['Abby', 'Bob', 'Chris'],
    'Active' : ['Y', 'Y', 'N'],
    'A' : [89, 92, np.nan],
    'B' : ['eye', 'hand', np.nan],
    'C' : ['right', 'left', 'right'] 
}

df = pd.DataFrame(data)
mask = (df['Active'] =='N') & (df['A'].isna())

df.loc[mask, 'A'] = 99
df.loc[mask, 'B'] = df.loc[mask, 'C']

print(df)

Now, the DataFrame will be updated correctly. Following is the output:

    Name Active     A      B      C
0   Abby      Y  89.0    eye  right
1    Bob      Y  92.0   hand   left
2  Chris      N  99.0  right  right
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