0

Given that i have a below datadrame:

dt = pd.DataFrame({"Cabin" : ["Hello","Morning",np.NAN,"BBC"]})

I wish to apply the following process on the Cabin column:

dt["Cabin"].map(lambda x: x[0] if x!=np.nan else np.nan )

But, i am facing with the below error:

TypeError: 'float' object is not subscriptable

I can understand the if x!=np.nan is not working, but i don't know how to fix it ?

The expected output is as following

expected_output = pd.DataFrame({"Cabin" : ["H","M",np.NAN,"B"]})
Jeff
  • 7,767
  • 28
  • 85
  • 138

0 Answers0