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

后端 未结 2 1965
长发绾君心
长发绾君心 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:21

    You could use apply like this:

    df.apply(lambda row: format_number(row), axis=1)
    

    You would need to specify the columns though in your format_number function:

    def format_number(row):
        row['Col1'] = doSomething(row['Col1']
        row['Col2'] = doSomething(row['Col2'])
        row['Col3'] = doSomething(row['Col3'])
    

    This is not as elegant as @BrenBarn's answer but it has an advantage that the dataframe is modified in place so you don't need to assign the columns back again

提交回复
热议问题