-1

I am new in Python and I would like to extract a certain kind of string data from my data frame. Here is my data frame:

enter image description here

I want to extract the first letter in the cabin column(like A,B,C,D...) and add it as a new feature column named as CabinCapital in the data frame. How could I do it? For instance, row 460 should have a feature CabinCapital which contains a letter "E".(if the cabin value is empty("NAN"), the cabinCapital is empty as well)

bigbounty
  • 16,526
  • 5
  • 37
  • 65

1 Answers1

1

Assuming the letter to extract is always the first character in the string, the following should help.

df['CabinCapital'] = df['Cabin'].str[0]

This also assumes your DataFrame is called df.

Ben
  • 856
  • 5
  • 9