Subtract a column from one pandas dataframe from another
dataframe, pandas, python, python-2.7, subtraction
Solution
rates.sub(treas.iloc[:,0],axis=0).dropna()
or
rates.sub(treas.squeeze(),axis=0).dropna()
Problem
I'm sorry for a dumb question, but I cannot find any way to do this easily. 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 ``` 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 `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? Thank you.