gis_grad_student
gis_grad_student

Reputation: 184

Use multiple columns of a dataframe for an operation and save the result in multiple columns

I did go through multiple StackOverflow posts to get an idea of how to solve this but couldn't come up with anything.

So, I have a dataframe with three attributes: id, X1, Y1. I need to pass each instance/entry of the dataframe to a function(e.g., func) which returns two values: X2, Y2. The operation basically looks like this:

X2, Y2 = func(X1, Y1)

I need to save the X2, Y2 for each entry as a new column so that the new dataframe looks like: id, X1, Y1, X2, Y2

I am not sure how to perform this with pandas. Could you please give me some pointers?

Thanks a lot for your effort and time!

Upvotes: 0

Views: 179

Answers (2)

constantstranger
constantstranger

Reputation: 9379

I believe this will do what your question asks (note that func() has been given an arbitrary sample implementation in this example):

import pandas as pd
df = pd.DataFrame({
    'X1' : [1,2,3,4,5],
    'Y1' : [2,2,3,3,4]
})
def func(a, b):
    return a - b, a + b
df[['X2', 'Y2']] = pd.DataFrame(df.apply(lambda x: func(x['X1'], x['Y1']), axis=1).tolist(), columns=['foo', 'bar'])
print(df)

Output:

   X1  Y1  X2  Y2
0   1   2  -1   3
1   2   2   0   4
2   3   3   0   6
3   4   3   1   7
4   5   4   1   9

Upvotes: 1

dasfacc
dasfacc

Reputation: 175

I'm pretty sure we need more details, but you can do this with

df.apply(func, axis=1, expand=True)

Better would be

df["X2"] = df["id"] + df["X1"] + df["Y1"]

I believe the latter is vectorized while the former would be run as a for loop

Hope this helps

Upvotes: 0

Related Questions