how to apply a function to multiple columns in a pandas dataframe at one time

后端 未结 2 1966
长发绾君心
长发绾君心 2020-12-15 23:35

I frequently deal with data which is poorly formatted (I.e. number fields are not consistent etc)

There may be other ways, which I am not aware of but the way I form

2条回答
  •  一生所求
    2020-12-16 00:10

    You can do df[['Col1', 'Col2', 'Col3']].applymap(format_number). Note, though that this will return new columns; it won't modify the existing DataFrame. If you want to put the values back in the original, you'll have to do df[['Col1', 'Col2', 'Col3']] = df[['Col1', 'Col2', 'Col3']].applymap(format_number).

提交回复
热议问题