pandas 从另一个熊猫数据框中减去一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20100717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtract a column from one pandas dataframe from another
提问by gt6989b
I'm sorry for a dumb question, but I cannot find any way to do this easily.
我很抱歉提出一个愚蠢的问题,但我找不到任何方法可以轻松做到这一点。
I have two pandasdata frames in Python 2.7, which are indexed by tenor:
我pandas在 Python 2.7 中有两个数据框,它们由 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
and
和
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 would like to subtract the treasfrom each column in ratesin the common indices where the data is present, and throw away the rest of the rows. How would I do that? Both rates - treasas well as rates.sub(treas)and rates.rsub(treas)produces NaNdata frames?
我想treas从rates数据所在的公共索引中的每一列中减去,并丢弃其余的行。我该怎么做?二者rates - treas以及rates.sub(treas)和rates.rsub(treas)产生NaN的数据帧?
Thank you.
谢谢你。
回答by Roman Pekar
rates.sub(treas.iloc[:,0],axis=0).dropna()
or
或者
rates.sub(treas.squeeze(),axis=0).dropna()

