![](/img/trans.png)
[英]Merging two dataframes with same column names but different number of columns in pandas
[英]Multiply two Pandas dataframes with same shape and same columns names
我有两个数据帧A,B,NxM形状。 我想将两者相乘,使A的每个元素与B的相应元素相乘。
e.g:
A,B = input dataframes
C = final dataframe
I want C[i][j] = A[i][j]*B[i][j] for i=1..N and j=1..M
我搜索但无法得到完全解决方案。
我想你可以用:
C = A * B
下一个解决方案是使用mul
:
C = A.mul(B)
样品:
print A
a b
0 1 3
1 2 4
2 3 7
print B
a b
0 2 3
1 1 4
2 3 2
print A * B
a b
0 2 9
1 2 16
2 9 14
print A.mul(B)
a b
0 2 9
1 2 16
2 9 14
A
和B
300k长度的计时 :
In [218]: %timeit A * B
The slowest run took 4.27 times longer than the fastest. This could mean that an intermediate result is being cached
100 loops, best of 3: 3.57 ms per loop
In [219]: %timeit A.mul(B)
100 loops, best of 3: 3.56 ms per loop
A = pd.concat([A]*100000).reset_index(drop=True)
B = pd.concat([B]*100000).reset_index(drop=True)
print A * B
print A.mul(B)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.