I have a pandas dataframe that if the value in column XNodes is 1 I need to perform a calculation to populate a new column PMNode2, else (value 0) I need to perform a different calculation to populate PMNode2.
I applied the following where statement. This works for the 0, but for the 1s an NaN is returned in the column. For the XNodes == statement, I have tried using 1, 1.0, '1.0' . The data type is float64 for all the columns involved. Is there something obviously wrong with how i've done this. Or should I be applying an if statement instead? i didn't have much luck there. Thanks
Nodes_2['PMNode2'] =(((Nodes_2['PMNode'] *.75) (Nodes_2['IntitFlow'] *.25)).where(cond=Nodes_2['XNodes']=='1.0'))
Nodes_2['PMNode2'] =((Nodes_2['PMNode'] *.75).where(cond=Nodes_2['XNodes']==0))
CodePudding user response:
Can you use the following statement for dataframe conditional assignment?
df.loc[df.A==xx, 'B'] = yy
So in your case:
Nodes_2.loc[Nodes_2['XNodes']==0, 'PMNode2'] = something
Nodes_2.loc[Nodes_2['XNodes']==1, 'PMNode2'] = something