Issue
I have a dataframe like:
import pandas as pd
import numpy as np
d = {'col1': [[1,2,3]]}
df = pd.DataFrame(d)
Now lets say I want to create two new columns, squared
and cubed
like:
df['squared'] = [np.array([i**2 for i in x]) for x in df['col1']]
df['cubed'] = [np.array([i**3 for i in x]) for x in df['col1']]
df
col1 squared cubed
0 [1, 2, 3] [1, 4, 9] [1, 8, 27]
Now I would want to do element-wise multiplication and sum with each element of the squared
and cubed
numpy arrays. How could I go about doing this an an efficient manner (if say I had a large amount of data?)
Solution
You could try matrix multiplication.
df.apply(lambda x: x['squared'] @ x['cubed'], axis=1)
Answered By - Chris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.