2

I have the following data frame:

AIRCRAFT
B738 (C-GKWJ)
A321 (C-FJNX)

and using this code to extract only TYPE,

def extract_type(aircraft):
    return aircraft.split(" ")[0]

data['TYPE'] = data['AIRCRAFT'].apply(lambda x: extract_type(x))

my expectation is:

AIRCRAFT       TYPE
B738 (C-GKWJ)  B738
A321 (C-FJNX)  A321

but im getting:

AIRCRAFT       TYPE
B738 (C-GKWJ)  B738 (C-GKWJ)
A321 (C-FJNX)  A321 (C-FJNX)

when testing extract_type function, it works fine

 def extract_type(aircraft):
     return aircraft.split(" ")[0]

    extract_type("B737 (C-GWJT)")

returns 'B737'

Why it doesn't work with data frame and lambda function? Thanks

1 Answer 1

2

You can use str.split

data['TYPE'] = data['AIRCRAFT'].str.split().str[0]

You get

    AIRCRAFT    TYPE
0   B738 (C-GKWJ)   B738
1   A321 (C-FJNX)   A321

You can also use str.extract though split is ideal

 data['TYPE'] = data['AIRCRAFT'].str.extract('(\w+)\s\(', expand = False)
Sign up to request clarification or add additional context in comments.

1 Comment

Glad that it worked. You can accept the answer if the issue is completely resolved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.