## Python Pandas Matrix Multiplication Multiple Operations in one

Question

I am trying to use the pandas matrix multiplication. I have four steps

- Create 2500 new columns by dividing 2500 columns by 2500 columns
- Use the result above and subtract 1 from all the columns
- Use the result of point 2 and divide all 2500 columns by 1 column
- Use the result of point 3 and add 1 column to all 2500

Is there a way to write this better, perhaps in a single line of code? It is currently quite slow and is CPU heavy.

The code snippet is below:

```
df_baseScenario[end_columns+ "fwd_rate"] = df_baseScenario[start_columns].divide(df_baseScenario[end_columns].values,axis=0)
df_baseScenario[end_columns+ "fwd_rate"] = df_baseScenario[end_columns+ "fwd_rate"].subtract(1)
df_baseScenario[end_columns+ "fwd_rate"] = df_baseScenario[end_columns+ "fwd_rate"].multiply(1/df_baseScenario['Calc_Period'].values,axis=0)
df_baseScenario[end_columns+ "fwd_rate"] = df_baseScenario[end_columns+ "fwd_rate"].add(df_baseScenario['Rate Index Spread'].values,axis=0)
```

Show source

## Answers to Python Pandas Matrix Multiplication Multiple Operations in one ( 2 )

I create code by text in question, no by sample code:

To make it a bit shorter and more readable, you can employ chaining as follows: