擅长:python、mysql、java
<h3>就这样做:</h3>
<pre><code>pd.concat([df1,df2], axis=1).sum(axis=1)
</code></pre>
<p><code>pd.concat</code>将合并2个(或更多)帧并基于索引进行匹配。<code>sum(axis=1)</code>仅对行进行求和。</p>
<h3>下面是一个工作示例:</h3>
<pre><code>#create the example data
df1 = pd.DataFrame({'index':[0,1,2,4],'value':[10,12,15,20]}).set_index('index')
df2 = pd.DataFrame({'index':[0,1,3,4],'value':[10,10,10,10]}).set_index('index')
</code></pre>
<p>以上将为您提供:</p>
<pre><code>In [7]: pd.concat([df1,df2],axis=1).sum(axis=1)
Out[7]:
index
0 20.0
1 22.0
2 15.0
3 10.0
4 30.0
dtype: float64
</code></pre>