从另一个 pandas 数据框中减去一列 [英] Subtract a column from one pandas dataframe from another

查看:195
本文介绍了从另一个 pandas 数据框中减去一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉一个愚蠢的问题,但我找不到任何方法可以轻松地这样做。

I'm sorry for a dumb question, but I cannot find any way to do this easily.

我有两个 code> Python 2.7中的数据框架,由索引索引:

I have two pandas data frames in Python 2.7, which are indexed by tenor:

In [136]: rates
Out[136]:
          A      A-    BBB+     BBB    BBB-      BB
3M   0.3150  0.3530  0.4960  0.6460  0.7910  1.9070
6M   0.3070  0.3560  0.5330  0.6740  0.8740  1.9170
1Y   0.3810  0.4470  0.6380  0.8970  1.1220  1.9900
2Y   0.7040  0.8690  1.0080  1.3510  1.6150  2.3230
3Y   1.0650  1.2870  1.4350  1.7950  2.0960  2.7590
4Y   1.5980  1.7920  1.9540  2.2660  2.6600  3.5890
5Y   2.0890  2.2660  2.4390  2.7890  3.2200  4.3280
7Y   2.9760  3.2010  3.2500  3.7600  4.3790  5.1970
8Y   3.3410  3.5410  3.5920  4.1270  4.7610  5.5170
9Y   3.5870  3.7400  3.9180  4.4630  4.9830  5.7710
10Y  3.7970  3.9410  4.1980  4.6440  5.1170  5.9630
15Y  4.6750  4.7290  5.3450  5.3440  5.3760  7.0900
20Y  5.3580  5.3760  5.5020  5.5850  5.5610  8.1920
25Y  5.2545  5.4055  5.4345  5.5435  5.5375  7.9935
30Y  5.1510  5.4350  5.3670  5.5020  5.5140  7.7950

In [137]: treas
Out[137]:
     2013-09-20 12:01:00
1M                 0.008
3M                 0.013
6M                 0.043
1Y                 0.104
2Y                 0.332
3Y                 0.688
5Y                 1.478
7Y                 2.109
10Y                2.735
30Y                3.762

I想在数据存在的共同索引中的价格中的每列中减去 treas ,并丢弃剩下的行。我该怎么办? rate-treas 以及 rates.sub(treas) rates.rsub treas)生成 NaN 数据框?

I would like to subtract the treas from each column in rates in the common indices where the data is present, and throw away the rest of the rows. How would I do that? Both rates - treas as well as rates.sub(treas) and rates.rsub(treas) produces NaN data frames?

谢谢。

推荐答案

rates.sub(treas.iloc[:,0],axis=0).dropna()

rates.sub(treas.squeeze(),axis=0).dropna()

这篇关于从另一个 pandas 数据框中减去一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆