Home > Software design >  df.insert results in NAN value
df.insert results in NAN value

Time:11-15

I try to assign a value to column depending on value from another column:

This is the code i use:

Position = pd.Series([])

for i in range(len(df_empty)):
    if df_empty["Rank"][i] > 2:
        Position[i] = int(1)
        
    else:
        Position[i] = int(0)

df_empty.insert(4, "Position", Position)
Return 3M Rank Position
0.001036 3 NAN
-0.031008 2 NAN
-0.284040 1 NAN

I want the result to be:

Return 3M Rank Position
0.001036 3 1
-0.031008 2 0
-0.284040 1 0

What seems to be wrong with my code?

CodePudding user response:

You can achieve the same logic using apply:

import pandas as pd
import numpy as np

dt = pd.DataFrame({"Return 3M":[0.001036, -0.031008, -0.284040], "Rank":[3,2,1]})

dt.assign(Position=dt.apply(lambda x:int(x["Rank"] > 2), axis=1))

CodePudding user response:

IIUC, You want to create Position column, If this is OK for you you can do this without for-loop and if...else and use numpy.where like below:

import numpy as np
import pandas as pd
df['Position'] = np.where(df['Rank']>2, 1, 0)
# Or if you want insert
df.insert(loc=0, column='Position_2', value=np.where(df['Rank']>2, 1, 0))
# Or without numpy
df['Position_3'] = (df['Rank'] > 2).astype(int)

CodePudding user response:

enter image description here

use apply function and pass your function to it

  • Related