5

I have a bunch of columns which requires cleaning in Pandas. I've written a function which does that cleaning. I'm not sure how to apply the same function to many columns. Here is what I'm trying:

df["Passengers", "Revenue", "Cost"].apply(convert_dash_comma_into_float)

But I'm getting KeyError.

1
  • 1
    Shouldn't it be df[["Passengers", "Revenue", "Cost"]]? You need to use a list of column names when you index your dataframe. Commented May 25, 2018 at 0:43

1 Answer 1

9

Use double brackets [[]] as @chrisz points out:

Here is a MVCE:

df = pd.DataFrame(np.arange(30).reshape(10,-1),columns=['A','B','C'])

def f(x):
    #Clean even numbers from columns.
    return x.mask(x%2==0,0)

df[['B','C']] = df[['B','C']].apply(f)
print(df)

Output

    A   B   C
0   0   1   0
1   3   0   5
2   6   7   0
3   9   0  11
4  12  13   0
5  15   0  17
6  18  19   0
7  21   0  23
8  24  25   0
9  27   0  29

​
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I made a list of it and now getting TypeError that it cannot convert series to <class 'float'>. But it clearly works when I supply only one column like df["Passengers"].apply(convert_dash_comma_into_float)
This solution does not work ...
@mathee This appears to work from this example. Both columns B and C have the even values sub with zeroes. per the function, f.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.