I have the following dataframe :
Y = list(range(5))
Z = np.full(5, np.nan)
df = pd.DataFrame(dict(ColY = Y, ColZ = Z))
print(df)
ColY ColZ
0 0 NaN
1 1 NaN
2 2 NaN
3 3 NaN
4 4 NaN
And this dictionary :
Dict = {
0 : 1,
1 : 2,
2 : 3,
3 : 2,
4 : 1
}
I would like to fill ColZ with "ok" if corresponding value of ColY through Dict is 2. Consequently, I would like the following dataframe :
ColY ColZ
0 0 NaN
1 1 ok
2 2 NaN
3 3 ok
4 4 NaN
I tried this script:
df['ColZ'] = df['ColZ'].apply(lambda x : "ok" if Dict[x['ColY']] == 2 else Dict[x['ColY']])
I have this error :
TypeError: 'float' object is not subscriptable
Do you know why ?