Pandas - 'Series' object has no attribute 'colNames' when using apply()

前端 未结 1 1887
刺人心
刺人心 2021-02-05 12:24

I need to use a lambda function to do a row by row computation. For example create some dataframe

import pandas as pd
import numpy as np

def myfunc(x, y):
    r         


        
1条回答
  •  醉话见心
    2021-02-05 13:13

    When you use df.apply(), each row of your DataFrame will be passed to your lambda function as a pandas Series. The frame's columns will then be the index of the series and you can access values using series[label].

    So this should work:

    df['D'] = (df.apply(lambda x: myfunc(x[colNames[0]], x[colNames[1]]), axis=1)) 
    

    0 讨论(0)
提交回复
热议问题