Daniel Chepenko
Daniel Chepenko

Reputation: 2268

Multiply two pandas dataframe with similar columns

my df1 with the shape (5050, 63) where every column holds video_id and row represent boolean value, wether user has watched it or not. My df2 with the shape (63,2), where the first row holds video_id and the second row video_duration

I want to multiply two dataframes, to calculate overall duration of videos, user has watched.

For this reason I transposed df2 and converted to numeric values.

df_video_t = df_video_info.transpose()
new_header = df_video_t.iloc[0]
df_video_t = df_video_t[1:]
df_video_t.columns = new_header
df_video_t = df_video_t.convert_objects(convert_numeric=True)

Then, I try to multiply df1 to df2,

df_1.mul(df_2_t, axis=0)

but I instead of multiplication result, I recieve NaN in every cell.

My columns of the df1 and row of df2 are not sorted. How should I do multiplication properly?

Upvotes: 0

Views: 417

Answers (1)

cs95
cs95

Reputation: 402483

It looks like you want to perform a dot product on the values. This should be pretty fast -

r = df_1.values.dot(df_2_t.values.T)

Upvotes: 1

Related Questions