Reputation: 137
I have a function returning a tuple of values, as an example:
def dumb_func(number):
return number+1,number-1
I'd like to apply it to a pandas DataFrame
df=pd.DataFrame({'numbers':[1,2,3,4,5,6,7]})
test=dumb_df['numbers'].apply(dumb_func)
The result is that test
is a pandas series containing tuples.
Is there a way to use the variable test
or to remplace it to assign the results of the function to two distinct columns 'number_plus_one'
and 'number_minus_one'
of the original DataFrame?
Upvotes: 0
Views: 76
Reputation: 1824
df[['number_plus_one', 'number_minus_one']] = pd.DataFrame(zip(*df['numbers'].apply(dumb_func))).transpose()
To understand, try taking it apart piece by piece. Have a look at zip(*df['numbers'].apply(dumb_func))
in isolation (you'll need to convert it to a list). You'll see how it unpacks the tuples one by one and creates two separate lists out of them. Then have a look what happens when you create a dataframe out of it - you'll see why the transpose
is necessary. For more on zip, see here : docs.python.org/3.8/library/functions.html#zip
Upvotes: 1
Reputation: 168
Method 1: When you don't use dumb function,
df[['numbers_plus_one','numbers_minus_one']]=pd.DataFrame(df.apply(lambda x: (x[0]+1,x[0]-1),axis=1).values.tolist())
Method 2: When you have test(i.e. series of tuples you mentioned in question)
df[['numbers_plus_one','numbers_minus_one']]=pd.DataFrame(test.values.tolist())
I hope this is helpful
Upvotes: 0