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.

Hannan
  • 1,171
  • 6
  • 20
  • 39
  • 1
    Shouldn't it be `df[["Passengers", "Revenue", "Cost"]]`? You need to use a list of column names when you index your dataframe. – user3483203 May 25 '18 at 00:43

1 Answers1

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

​
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 1
    Thanks, I made a list of it and now getting TypeError that it cannot convert series to . But it clearly works when I supply only one column like df["Passengers"].apply(convert_dash_comma_into_float) – Hannan May 25 '18 at 02:10
  • 1
    This solution does not work ... – mathee Mar 01 '21 at 21:57
  • @mathee This appears to work from this example. Both columns B and C have the even values sub with zeroes. per the function, f. – Scott Boston Mar 02 '21 at 01:32