Reputation: 5911
I want to compute a row-wise dot product between two DataFrames. In my situation, the column names are the same, but the order might be a little different. (And I want to avoid reordering the columns.)
Is it possible to do a "named matmul" with these two DataFrames?
Specifically I have:
df_1 =
A B
index1_0 100 200
index1_1 300 400
index1_2 ...
...
df_2 =
B A
index2_0 0.2 0.8
index2_1 0.6 0.4
index2_2 ...
...
and I wanna get:
index2_0 index2_1
index1_0 120 160
index1_1 320 360
...
Is there a nice way to do this using the column names?
I'm aware that I can reorder the columns so they line up, and then do matmul with @
, but i'm wondering if there's a nicer, more intuitive (readable, maintainable) way.
Upvotes: 1
Views: 320
Reputation:
You can first align the DataFrames; then use dot
:
out = df1.dot(df2[df1.columns].T)
Output:
index2_0 index2_1
index1_0 120.0 160.0
index1_1 320.0 360.0
Upvotes: 3