Using Python to Analyze Real Estate Investments
By: Anthony Morast
Tags: Passive Income, Real Estate Investments, Real Estate, Python, Investing

Buy and hold real estate investing can be very satisfying and, oftentimes, very lucrative. In his book, One Up on Wall Street, even legendary stock picker Peter Lynch recommends buying a home before picking individual stocks in the stock market due to its investment potential (although some of his message comes off slightly sarcastic). Unlike with stocks and bonds purchasing real estate allows you to use a large amount of leverage with most investments requiring 20–25% as a down payment (this is a majority of the cash required to close the deal with the smaller portion being closing costs). Using 20% as a down payment allows you to purchase an asset worth 5x the amount you actually have to spend so a 2% increase in the home’s value results in a 10% return on the cash you have invested (in the ideal case). Furthermore, unlike the leverage available for stocks and bonds, your mortgage servicer isn’t going to be calling you and demanding more cash if the price of your real estate fluctuates due to market conditions. I’m not saying to avoid stocks and bonds as I’m an avid investor myself, but I think real estate investing is certainly worth a look.

One key to success in real estate investing (as with any type of investing) is learning how to determine a good deal from a bad deal. For real estate, the property’s condition, location, and other qualitative factors largely affect the potential income of a property. To accurately determine these factors knowledge and experience in a specific market is usually required. However, some aspects that are under the control of novice and experienced real estate investors alike involve the numbers. Quantitative factors such as determining the maximum amount to pay for a property, the potential cash flow, and the returns on the cash invested can easily be determined with the use of a calculator. In this post, Python will be used to analyze real estate deals to help weed out the good deals from the bad deals.

Real Estate Return Metrics

Cash-on-Cash Returns

Two common metrics for determining if a property is worth investing in are the cash-on-cash (CoC) return and the capitalization rate. The CoC return is a return rate that compares the cash earned from the property (after expenses) to the total amount of cash invested. Typically, the cash invested is the sum of the down payment and the closing costs; this is, essentially, all of the up-front cash required to close the deal, if additional capital is needed to make the property “rent-ready” those should be included too. The formula for CoC return is as follows

The CoC return is the annual yield for owning the property assuming no additional cash is required from year to year.

For example, assume a property is purchased with a 30-year, 4% interest rate mortgage for $100,000 having a 20% down payment (which equals $20,000) and that $2,500 is paid in closing costs. Let’s also assume the vacancy rate is 10%, maintenance (as a percent of rent) is 5%, you’re paying a property management team 10%, annual taxes and insurance are $1,200 (combined), and that you can generate $1,000 per month of rental income. In this case, a typical month’s expenses are

The cash flow for the property is simply the rental income less any expenses, i.e.

Therefore, in this example, the property will generate

in monthly cash flow. Multiply by 12 to get the annual cash flow of $2,070.96. Since $22,500 was required to purchase this property (for the closing costs plus the down payment) the CoC return works out to

Capitalization Rate

A second popular metric used when analyzing real estate returns is the capitalization rate (or cap rate). The cap rate represents the yield of a property over one year assuming that the property is purchased with cash rather than with a loan. Or, another way to think about it is that it’s the return on the property once it’s paid off. Thus, the cap rate uses the same cash flow formula presented above but with the mortgage payment added back in and divides this number by the market value of the property, that is

For the example above, the cap rate is

Benefits of Using a Python Script

As seen here, these calculations can become tedious when you have to work them out by hand (especially if you’re evaluating multiple deals). Fortunately, there are many online calculators that will do this for you. However, one major drawback of these calculators is that you will need to manually enter every potential real estate deal, perform the calculation, and try to save the results somewhere to compare them with other deals.

In the Python script presented below, this procedure is completed using a CSV file as input containing all of the details for the properties you want to evaluate. The results are saved and a second Python script is provided to display the results in a more readable format. This implementation is especially useful if using an API to gather the real estate data which could be used to generate this input CSV file. This will do away with much of the manual work when analyzing these quantitative factors. Furthermore, the Python script’s output includes other important data such as the cash required to close, the mortgage payment, the minimum monthly expenses, and the maximum purchase price (based on a minimum of $100 in monthly cash flow).

Analyzing Real Estate Returns with Python

Analysis Formulas

To begin, the formulas above will need to be converted to Python code, fortunately, since these are relatively simple formulas, this is easily done with the functions below.

Getting Data

With these functions in place, we now need a way to get the data for the Python logic. In this particular implementation, I provide two different ways to do this: by prompting the user for input and via CSV file as mentioned above. The CSV file processing is handle with the pandas Python library. To prompt for the data the following function is used

The user is prompted for all of the required data to perform the analysis. This is then returned as a pandas dataframe so that no special processing is needed between the CSV and prompted runs of the script. One thing to note is that the dictionary keys shown above (the data dictionary object) are the required headers for the CSV file read by the program (they can be in any order).

Parameters

There are two optional parameters to the script that determine whether to use a CSV file for input or to prompt for data and the output filename. These parameters are

Two examples runs of the script are provided below.

The CSV file needs to have the following headers: “ID”, “Purchase Price”, “Rental Income”, “Closing Costs”, “Loan Amount”, “Interest Rate”, “Vacancy Rate”, “Loan Term”, “Maintenance Expense”, “Property Management Fees”, “Annual Property Taxes”, and “Annual Property Insurance”. For example, the CSV file below is used for the example run in this post

By selecting “View Raw” in the lower right of the gist above, this file can be saved and used as a template for your execution of the Python script.

Analysis

After the data is provided to the script, either via CSV or command prompt, the results are created by calculating the formulas described above along with some other useful data

These calculations a pretty rudimentary and explained by the comments throughout the code so I will exclude an explanation here to prevent the post from getting too long.

Running this analysis with the example CSV file shown above will produce the results.csv file below

Since this is a little hard to read in tabular format the script below was created to make it easier to see the results.

If using a custom output filename in the analysis, that filename will need to be provided here as well via the filename parameter:

Running this display script for the results in the table above produces this output

Additionally, since the results produced from the analysis are in CSV format originally they can be further analyzed in a popular programming language, such as Python, or spreadsheet software, such as Microsoft Excel or OpenOffice Calc, to help filter out investments that don’t have the potential an investor is expecting.

One thing to note is that this analysis could be run over and over for the same property. The same property could be listed multiple times in the input CSV file with slightly different parameters for the interest rate, down payment, maintenance fees, etc. which would allow the investor to analyze an investment property’s potential under a different set of assumptions.

Conclusion

In this post, two popular metrics for analyzing a real estate investment are provided. As seen above, calculating these metrics can quickly become a tedious task. Using Python this tedious task is transformed into simply aggregating data for your potential real estate investments and running some scripts to generate results. This functionality has the potential of saving a real estate investor hours upon hours each year in analyzing investments.

To make this analysis more convenient, one could use a real estate API to programmatically gather data on real estate for sale in the area you’re investing in. This data could be used to generate the input CSV file for the analysis which would, more or less, fully automate this part of the investment process. The full code for this project is provided below.

Full Code

analysis.py

display_results.py