Combining Excel and Python
*****************************
Use Python from Excel and use Excel from Python. Learn to use the two tools together flexibly to enable using the most effective tool for each portion of the model.
Resources
============
- :download:`Slides - Combining Excel and Python `
- :download:`Lecture Notes - Combining Excel and Python `
- :download:`Read Write Excel Pandas `
- :download:`MSFT Financials `
- :download:`Combining Excel and Python `
- :download:`Example Workbook `
- :download:`xlwings Lab `
Introduction to Combining Excel and Python
=============================================
.. youtube:: HbIVMdbZuzQ
:height: 315
:width: 80%
:align: center
|
Notes
--------
- At this point in the course, you should feel comfortable using both Python and Excel to create models to solve problems
- Now it is time to learn how to combine the two tools for the maximum flexibility, power, and convenience
- We will cover two approaches to integrating the two: using Pandas and using xlwings
- We are also about to learn Monte Carlo simulation, which can be done easily in Python but would require using VBA or an extension in Excel. Using this combination we can have the model in Excel and run Monte Carlo simulations on it in Python
- The Pandas approach is simpler but is much more limited, basically you can read in Excel workbooks and you can output an entire workbook or sheet
- The xlwings approach gets a bit more complicated but allows to have a connection between Excel and Python and transfer individual values or entire tables back and forth with an existing workbook
Transcript
-------------
.. raw:: html
- 00:03: hey everyone
- 00:04: nick dear burtis here teaching you
- 00:05: financial modeling today
- 00:07: we're going to be starting a new lecture
- 00:10: segment
- 00:10: on combining excel and python so in this
- 00:14: video we're just going to introduce the
- 00:16: topic
- 00:17: and talk about why we're combining the
- 00:19: two why we're just covering this now
- 00:22: etc so
- 00:26: we so far have
- 00:29: built out a competency in working
- 00:32: with both excel and python now
- 00:36: at this point in the course you should
- 00:38: feel comfortable
- 00:39: working with both in excel and python to
- 00:42: build out models for whatever problem
- 00:46: that you're trying to solve and now that
- 00:49: we feel comfortable
- 00:50: with each of the two tools we can start
- 00:54: looking at how we can
- 00:55: combine the two tools
- 00:58: um because excel and python they both
- 01:02: have different advantages and
- 01:03: disadvantages
- 01:04: and for any particular problem that
- 01:06: you're going to tackle
- 01:08: uh one of the two tools may be better
- 01:10: suited
- 01:11: for different parts of the problem
- 01:15: and so it's really an ideal setup to be
- 01:18: able to flexibly switch back and forth
- 01:20: between the two
- 01:22: to be able to use whichever tool is
- 01:25: most effective for the job
- 01:28: so that is a big thing that
- 01:31: we're building towards in this course is
- 01:34: not only knowing how to build financial
- 01:36: models in both excel and python
- 01:39: but also knowing which one to use and
- 01:41: when
- 01:42: so that you can most effectively work
- 01:46: on the problems that you're trying to
- 01:47: solve
- 01:52: different problems we can break them
- 01:53: down and do some parts with
- 01:55: each and um this will probably also
- 01:59: be a breath of fresh air for those that
- 02:02: have really struggled with the python
- 02:04: side of things and
- 02:05: want to do as much as possible in excel
- 02:08: well this allows you to
- 02:10: build most of your model model in excel
- 02:12: and then
- 02:13: add on these additional capabilities
- 02:15: that python has
- 02:18: to your excel model
- 02:22: so now you're going to be able to just
- 02:25: flexibly go back and forth between the
- 02:28: two
- 02:29: and this is going to be really useful as
- 02:32: we go to learning monte carlo simulation
- 02:35: the next major
- 02:36: topic because there's not
- 02:39: a easy way to do that in excel without
- 02:42: going to vba so we can use python to run
- 02:47: monte carlo simulations on both our
- 02:48: python
- 02:49: and excel models
- 02:52: and as far as how we can combine
- 02:56: python and excel we'll talk about two
- 02:58: major approaches
- 03:00: we'll talk about using pandas and we'll
- 03:04: also talk about
- 03:06: excel wings as two libraries which
- 03:09: are going to be able to help us combine
- 03:11: the two
- 03:12: where pandas is a simpler integration
- 03:15: where we can just
- 03:18: pass a workbook back and forth basically
- 03:21: read that in from a workbook output to a
- 03:24: workbook
- 03:27: where as when we use excel wings then we
- 03:30: can really flexibly go back and forth
- 03:32: between the two
- 03:33: write individual values uh back and
- 03:36: forth between excel and python
- 03:38: run uh excel functionality from python
- 03:42: and many more use cases so
- 03:46: we'll explore each of these two methods
- 03:49: in turn
- 03:50: and that will be the focus of the
- 03:52: following videos
- 03:54: so thanks for listening and see you next
- 03:58: time
|
Combining Excel and Python using Pandas
==========================================
.. youtube:: GrzJSNrP6ns
:height: 315
:width: 80%
:align: center
|
Notes
--------
- If you have a Python model and you just want to load some data in from Excel, Pandas is probably your best choice
- If you have an Excel model and you collect the data using Python, this is also a good choice
- If you want to have some parts of your model in Excel and some parts in Python, you should probably look to xlwings
- BE CAREFUL WHEN WRITING TO WORKBOOKS as it will replace what is there. It could overwrite your Excel model. THERE IS NO UNDO (back up your work)
Resources
------------
- :download:`Read Write Excel Pandas `
- :download:`MSFT Financials `
Transcript
-------------
.. raw:: html
|
Combining Excel and Python using xlwings
===========================================
.. youtube:: LYzVXHCJs40
:height: 315
:width: 80%
:align: center
|
Notes
--------
- xlwings is a package that makes it quite easy to combine Excel and Python in ways that should work for nearly every use case
- This can be done without xlwings using the Microsoft COM API in Python, but xlwings is far more convenient
- We are focusing here on only manipulating Excel from Python, but I encourage you to explore running Python from Excel on your own time
- Now with xlwings, anything that you can do in Excel, you can make it happen from Python
- It is easy to transfer individual values, ranges, and tables back and forth between Excel and Python
- For those who have a lot of difficulty with Python and feel comfortable in Excel, xlwings allows building out the core model in Excel and adding extensions such as Monte Carlo simulation, sensitivity analysis, and scenario analysis in Python
Resources
------------
- :download:`Combining Excel and Python `
- :download:`Example Workbook `
- :download:`xlwings Lab `
Transcript
-------------
.. raw:: html
|