0

I have an issue with a dataframe df as follows:

    NODEID  BigNode    Node1        Node2       Node3
0   500-1   NaN         2000.0       NaN        NaN
1   500-2   NaN         NaN          3000.0     NaN
2   500-3   NaN         NaN          NaN        4000.0
3   500-4   50000.0     NaN          NaN        NaN
4   500-5   60000.0     NaN          NaN        NaN

What I need is to create a new column (Node-F), so that: If BigNode value exists then write a specific constant value (SomeValBN) to Node-F Similarly, for the rest Node1, Node2, Node3, to write respective values to Node-F In each of BigNode, Node-1, Node-2, Node-3 each time will have a value.

    NODEID  BigNode    Node1        Node2       Node3       Node-F
0   500-1   NaN         2000.0       NaN        NaN        SomeVal1
1   500-2   NaN         NaN          3000.0     NaN        SomeVal2
2   500-3   NaN         NaN          NaN        4000.0     SomeVal3
3   500-4   50000.0     NaN          NaN        NaN        SomeValBN
4   500-5   60000.0     NaN          NaN        NaN        SomeValBN

In EXCEL i solved the above with a nested if() statement:

=IF(B2>0;"BN1";(IF(C2>0;"N1";(IF(D2>0;"N2";(IF(E2>0;"N3";0))))))) 
where, BN1="SomeValBN", N1="SomeVal1" etc

The above will be stored in a new dataframe df_1, without index.

I am completely stuck! Thank you

Paradigm
  • 149
  • 1
  • 2
  • 10

1 Answers1

1

We can achieve this by using conditional statements.

Let df be your dataframe:

for i in range(df.shape[0]):
    if(df.at[i, 'BigNode'] > 0):
        df.at[i, 'Node-F'] = 'SomeValBN'
    elif(df.at[i, 'Node1'] > 0):
        df.at[i, 'Node-F'] = 'SomeVal1'
    elif(df.at[i, 'Node2'] > 0):
        df.at[i, 'Node-F'] = 'SomeVal2'
    elif(df.at[i, 'Node3'] > 0):
        df.at[i, 'Node-F'] = 'SomeVal3'
    else:
        df.at[i, 'Node_F'] = '0'
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83