Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply function to all columns in a my dataframe

Tags:

python

pandas

I have trouble to apply my custom function to a dataframe:

df = pd.DataFrame({
        "a": ["related-1", "related-0", "related-1"],
        "b": ["request-1", "request-0", "request-1"],
        "c": ["offer-1", "offer-0", "offer-1"],
    })

def clean_column(df, column):
    return df[column].apply(lambda x: re.sub(".*-", "", x)).astype("int64")

clean_column(df, "a") # This works

I want to apply this function to multiple columns:

df.applymap(lambda x: clean_column(df, x))

Error: KeyError: 'related-1'

Now sure what I am doing wrong here, can somebody tell me the reason this does not work?

like image 858
Data Mastery Avatar asked Nov 18 '25 10:11

Data Mastery


1 Answers

apply takes the whole column as input, not just the name. So you want:

def clean_column(column):
    return column.apply(lambda x: re.sub(".*-", "", x)).astype("int64")

df.apply(clean_column)

However, in your case, you do apply again for each column. In other words, you want to apply a function to all cells. That is applymap:

df.applymap(lambda x: re.sub(".*-", "", x)).astype("int64")

Output:

   a  b  c
0  1  1  1
1  0  0  0
2  1  1  1
like image 134
Quang Hoang Avatar answered Nov 20 '25 23:11

Quang Hoang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!