pandas problem when assigning value using loc
Question:
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
Answers:
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
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 |
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