0

I hope to be clear on the questions, but let me explain me better, I have this dataframe:

import pandas as pd
  m = pd.DataFrame({'A': (1, 2, 3), 
                    'B': ([0, 1, 2], [3, 4, 5], [6, 7, 8])})

My objective is to obtain the column z which is each y-array squared plus the x-integer corresponding the position in the columns. Maybe you can get it in that way:

  m = pd.DataFrame({'A': (1, 2, 3), 
                    'B': ([1, 2, 3], [4, 5, 6], [7, 8, 9]),
                    'C': ([2, 5, 10], [18, 27, 38], [52, 67, 84])})

I can do it in R using this code:

m <- m %>% mutate(z = map2(x,y, ~map2_dbl(.x,.y, ~ (.x + .y^2))))

Is there something similar in Python? Regards

2
  • What is the issue, exactly? Have you tried anything, done any research? What does your data actually look like? Storing lists in a DataFrame is not ideal. Commented Apr 5, 2020 at 21:29
  • Hi! Thanks for the question. I know that the way I stored that data is not the best, however, this is a dataframe with is composed of different discount rates for cashflows with different periods (some have 20 years, others 10 and so on). I must to stored in that way in order to deliver the NPV and have each cashflow presented. If you know a better way to store the data, I am open to suggestions. Regards Commented Apr 7, 2020 at 0:10

2 Answers 2

2

This could get pretty inefficient by using python lists. I'd suggest you to use numpy here:

import numpy as np

m['C'] = (np.stack(m.B.values)**2 + m.A.values[:,None]).tolist()

print(m)

A          B             C
0  1  [1, 2, 3]    [2, 5, 10]
1  2  [4, 5, 6]  [18, 27, 38]
2  3  [7, 8, 9]  [52, 67, 84]
Sign up to request clarification or add additional context in comments.

1 Comment

Hey! Thanks for the answer! That was pretty handy
2

In pandas , and since you have object in your column, which will make most of the method from pandas not work, we use for loop here. This method should be fast enough ~

m['C']=[(x + np.array(y)**2).tolist() for x , y in zip(m.A,m.B)]

1 Comment

Thanks for the answer. I didn't know about this zip module. Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.