Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract a column from one pandas dataframe from another

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.

like image 690
gt6989b Avatar asked Nov 20 '13 15:11

gt6989b


People also ask

How do I subtract one column from a different DataFrame?

We can create a function specifically for subtracting the columns, by taking column data as arguments and then using the apply method to apply it to all the data points throughout the column.

How do you subtract values from one DataFrame from another panda?

The sub() method of pandas DataFrame subtracts the elements of one DataFrame from the elements of another DataFrame. Invoking sub() method on a DataFrame object is equivalent to calling the binary subtraction operator(-). The sub() method supports passing a parameter for missing values(np. nan, None).

How do I subtract two columns in a Dataframe in Python?

How to Subtract Two Columns in Pandas DataFrame You can use the following syntax to subtract one column from another in a pandas DataFrame: #subtract column 'B' from column 'A' df ['A-B'] = df.A- df.B The following examples show how to use this syntax in practice.

How to subtract Dataframe in pandas?

Pandas dataframe.subtract () function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs. Attention geek!

What is ‘a-B’ column in a pandas Dataframe?

The new column called ‘A-B‘ displays the results of subtracting the values in column B from the values in column A. If we subtract one column from another in a pandas DataFrame and there happen to be missing values in one of the columns, the result of the subtraction will always be a missing value:

How to add a new row to a Dataframe?

It looks like you want to create new rows. You can index the dataframe by Account which also has the advantage that the remaining columns are the things you want to subtract. Then subtract and add a new row.


1 Answers

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

or

rates.sub(treas.squeeze(),axis=0).dropna()
like image 58
Roman Pekar Avatar answered Nov 14 '22 22:11

Roman Pekar