#1 Read fundamental data from a CSV in Python (Python Financial Analysis)
#1 Read a CSV
In this series, I’m going to introduce financial analysis techniques you can use in Python.
Financial analysis in programming is though to be hard for people who have no backgrounds in coding. We need to setup the environment and install necessary packages first, and more over, we have to learn coding. But I thing those are not that difficult if explained step by step. This series of stories is made for those people.
Along with writing this documents in Medium, I’ll make corresponding YouTube videos like the one shown at the top.
NOTE: This story is made for Windows 10 users or Ubuntu users. But I guess the basics are the same also on a Mac computer.
List of articles
1. Python Financial Analysis
1 Read fundamental data from a CSV in Python
2 Handling table like data in Python with DataFrame
3 Make graphs of stock price in Python
4.1 Make custom market index — prerequisites
4.2 Make custom market index — make your own index
4.3 Make custom market index — market cap based index
5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices
5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices
5.3 Find companies that lost or gained from the COVID19 pandemic
2. Python Data Analysis Basics (easiest ways)
1. Install Python3
Windows 10 or 11
You can download an installer of the latest Python from this web site linked below. After downloading the installer (.exe file) run it by clicking it twice. Then the installer launches. You can follow the installer just selecting “ok” at each phase. Note that, if you are asked to install “pip installer”, then choose to install it. It’s necessary to install additional Python tools (called “packages” ) later.
Ubuntu 18 or later
Don’t worry about this. It’s already installed on your computer by default. This is why Ubuntu is said to be a best OS for developments.
2. Install Python packages
You can’t do much things just with python itself. You have to install additional tools for handling financial data. At the beginning of your learning, you need just the following three packages.
- NumPy (for doing math things)
- Pandas (for handling table like data)
- Matplotlib (for making graphs)
You can install them with the “pip” installer. The python installer installs the pip installer as well at the same time, so you should have it on your computer. The pip installer works in the command line (black windows), so you have to open it first. On windows, you can type “cmd” on the Windows search box. Then on the command line type
“> pip install numpy”
“> pip install pandas”
“> pip install matplotlib”
On Ubuntu you have to be the sudo user, and use pip3 instead
“ > sudo pip3 install numpy” …
Read a CSV file
1. Download data set
The data set for financial analysis can be downloaded from the link below. In this story we use just the file named “meta.csv”. So download it to the same place where you make a Python script.
2. Make a Python script
On the same folder where you downloaded the file “meta.csv”, make an empty text file and change the extension from “.txt” to “.py”. You will be warned, but ignore it. Any file name is ok, but I named it “read_csv.py”. It’s better to follow it.
3. Import packages
We first import necessary tools with the “import”. We assigned aliases with the “as”, such that we don’t need type the package name every time we use them. Thus, if we want to use the sine function of “numpy”, for example, we use it in a way like “np.sin(…)” rather than “numpy.sin(…)”. It’s just for more convenience.
The fourth package “sys” is used for anything. We will see its usage later.
4. Read the CSV file “meta.csv”
Finally we read a CSV file with the “read_csv” function of Pandas. If you want to access a function defined in a package, you use dot “.” to access it. In this example, the package is Pandas, which is assigned an alias “pd”.
We give the path to the CSV file. Because the file is saved in the same folder where this Python script is saved, you can specify the path as a “relative path”. In a relative path “/” means “the same folder”. Thus, the path to the CSV file is “./meta.csv”. The returned value “meta” is a variable that has a data type “DataFrame”. DataFrame is a data type defined in Pandas, which handles table like data. Then we print the contents of “meta” with the “print()“ function to see if the program works.
5. Run the program
Usually, Python programs are executed on a command line. You can open the command line by typing “cmd” on the navigation on top of the explore.
Then on the command line, type “python read_csv.py” to execute the script. The output would be like this:
You can see that it’s the same as the one you see on Excel: