# Calculating IRR in Python

Calculation of IRR in Python is easy with the `numpy` module.

Why do we need to calculate the IRR or Internal rate of returns?

Let us suppose a manager has an opportunity to invest in two projects, but can only choose one project. The projects are:

Project 1 requires \$800 million in investment today, but it will pay 200,250,300,350,400 million in payments each year for the next 5 years.

Project 2 required \$500 million in investment today, but it will pay 150,170,178,250,300 in payments each year for the next 5 years.

How would the manager decide which project to choose? To solve this problem we need to calculate the IRR of the two projects. We show the solution below.

First let us import the libraries

``````import pandas as pd
import numpy as np``````
``````project1_cf = pd.DataFrame({"Year":np.arange(0,6),
"cf": [-800,200,250,300,350,400]})
project2_cf = pd.DataFrame({"Year":np.arange(0,6),
"cf": [-500,150,170,178,250,300]})
print(project1_cf)``````
``````##    Year   cf
## 0     0 -800
## 1     1  200
## 2     2  250
## 3     3  300
## 4     4  350
## 5     5  400``````
``print(project2_cf)``
``````##    Year   cf
## 0     0 -500
## 1     1  150
## 2     2  170
## 3     3  178
## 4     4  250
## 5     5  300``````
``````irr1 = np.irr(project1_cf["cf"])
irr2 = np.irr(project2_cf["cf"])
irr_df = pd.DataFrame({"Name":["Project1", "Project2"],
"IRR":[irr1, irr2]})
print(irr_df)
``````
``````##        Name       IRR
## 0  Project1  0.221603
## 1  Project2  0.267620``````

From the above table we can see that project 1 has an IRR of 22.16% and project 2 has an IRR of 26.76. Even though project 1 offers higher cash flows, project 1 has a lower internal rate of returns. If the managers goal is to choose the project that maximizes profitability then he/she should choose project 2.