Skip to content Skip to sidebar Skip to footer

Pandas - 'series' Object Has No Attribute 'colnames' When Using Apply()

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): return x + y colNam

Solution 1:

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)) 

Post a Comment for "Pandas - 'series' Object Has No Attribute 'colnames' When Using Apply()"