I have a function that returns a dict object and I would like to take advantage of pandas/numpy's ability to perform columnwise operations/vectorization for this function across every row of a dataframe. The inputs for the function are specified in the dataframe and I want the outputs of the function to become new columns on the existing dataframe. Below is an example.
def func(a, b, c): return { "a_calc": a * 2, "b_calc": b * 3, "c_calc": c * 4 }
df = pd.DataFrame([{"a":1, "b":2, "c": 3}, {"a": 4, "b": 5, "c": 6}]) a b c 0 1 2 3 1 4 5 6
Desired Output:
a b c a_calc b_calc c_calc 0 1 2 3 2 6 12 1 4 5 6 8 15 24
I was reading this answer and it got most of the way there but I couldn't quite figure out how to do it for when the function returns a dict object with the desired column names as the keys within the dict.
https://stackoverflow.com/questions/66978570/creating-multiple-pandas-columns-from-function-returning-dict April 07, 2021 at 09:53AM
没有评论:
发表评论