I can get this working
df['col_A'] = df.apply(lambda x: getSingleValue(x['col_X']), axis=1)
And also when my function returns tuple
df['col_A'] = df.apply(lambda x: getaTuple(x['col_X'])[0], axis=1)
df['col_B'] = df.apply(lambda x: getaTuple(x['col_X'])[1], axis=1)
But, I need to know if there is a way to apply the tuple output getaTuple()
to multiple columns of the data frame using a single function call rather than calling getaTuple
multiple times for each column I am setting the value.
Here is an example of input and output
df = pd.DataFrame(["testString_1", "testString_2", "testString_3"], columns=['column_X'])
def getaTuple(string):
return tuple(string.split("_"))
In [3]: iwantthis
Out[3]:
col_X col_A col_B
0 testString_1 testString 1
1 testString_2 testString 2
2 testString_3 testString 3
FYI, This is similar to how to apply a function to multiple columns in a pandas dataframe at one time
but not duplicate as in my case I need to pass col_X
as input to my function.