Issue
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 |
Solution
You can implement this by creating a boolean mask using the condition: Where the 'Active'
column is 'N'
and the 'A'
column has missing values (np.nan
). We can then use this mask to filter rows in the DataFrame
.
First, we will replace the values in column 'A'
with 99
for rows where the mask condition is True
. Then, replace the values in column 'B'
with the corresponding values from column 'C'
where the mask condition is True
.
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
Answered By - Bilesh Ganguly
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.