Sarah
Sarah

Reputation: 617

How to apply lambda function to column of dataframe correctly?

I have a dataframe that looks like this:

data = {'Name':['Tom #111', 'nick #1313', 'krish', 'jack #2 lol'],
        'Age':[20, 21, 19, 18]}

df = pd.DataFrame(data)

And I want to apply a function to get rid of any hastag+numbers. My code looks like this:

df['Name'].apply(lambda x: re.sub("#[-+]?[0-9]+", " ", str(df['Name'])))

However this comes out like this:

0 0 Tom
1 0 Tom
2 0 Tom
3 0 Tom

My expected output is:

0 0 Tom
1 0 nick 
2 0 krish
3 0 jack lol

I tried to use axis=1 in lambda function but it gives me an error of:

TypeError: <lambda>() got an unexpected keyword argument 'axis'

How can I fix this?

Upvotes: 1

Views: 1101

Answers (2)

Vaishali
Vaishali

Reputation: 38425

You can use pandas string methods,

df['Name'] = df['Name'].str.replace('#\d+', '')

    Name        Age
0   Tom         20
1   nick        21
2   krish       19
3   jack lol    18

If you want to know how to use apply (definitely not preferred over str methods),

df['Name'] = df['Name'].apply(lambda x: re.sub('#\d+', '', x))

Upvotes: 3

Błotosmętek
Błotosmętek

Reputation: 12927

df['Name'] = df['Name'].apply(lambda x: re.sub("#[-+]?[0-9]+", " ", x))

1) note where x is used

2) you need to store the result back into the dataframe

Upvotes: 2

Related Questions