How to Get Stock Price Data Using Python

Are you looking to retrieve stock market data using Python? You’re at the right place. In this article, you’ll learn how to get stock market data using Python. You can further use the data to analyze, visualize, and get insights from it.

You’ll be using theyfinancePython library to get the current and historical stock market price data from Yahoo Finance.

4

Installing Required Libraries

Yahoo Financeis one of the widely used platforms that provides stock market data. You can easily download the dataset from their website, but if you want to access it directly from a Python program, you can use theyfinancelibrary. To install yfinance using pip, you need to run the following command at a command prompt:

The yfinance Python library is free to use and it does not require an API key.

A collage showing stills from The Walking Dead, The Wire, House of Cards, Line of Duty, Manifest, and the Man in the High Castle

The code used in this project is available in aGitHub repositoryand is free for you to use under the MIT license.

Get Current Stock Price Data

You need to have the ticker of the stock for which you want to extract the data. In the following example, we’ll be finding the market price and previous close price for GOOGL.

This produces the following output:

An iPhone is shown with the account section grayed out in the settings menu

This example uses theregularMarketPriceandregularMarketPreviousCloseproperties to get the required data. The yfinance library provides numerous other properties that you can explore. These include zip, sector, fullTimeEmployees, longBusinessSummary, city, phone, state, and country. You can get the complete list of the available properties using this code:

Get Historical Stock Price Data

You can get all the historical price data by providing the start date, end date, and ticker.

The above code will fetch the stock price data from 2020-01-01 to 2022-01-01.

A clean hallway floor in a home.

If you want to pull data of multiple tickers at once, you can do so by providing the tickers in the form of a space-separated string.

Transforming Data for Analysis

In the above dataset,Dateis the index of the dataset and not a column. To perform any data analysis on this data, you need to convert this index into a column. Below is how you’re able to do that:

This transformed data is the same as the data you would have downloaded from Yahoo Finance.

A line graph displayed on an ipad

Storing the Received Data in a CSV File

You canexport a DataFrame object to a CSV fileusing theto_csv()method. Since the above data is already in the form of a pandas DataFrame, you can export the data into a CSV file using the following code:

Pandas is the widely used data-analysis Python library. If you’re not much comfortable with this library, you should get started withbasic operations using Pandas.

Visualize the Data

The yfinance Python library is one of the most convenient libraries to set up, fetch data, and perform data analysis tasks with. You can use this data to visualize results and capture insights using libraries like Matplotlib, Seaborn, or Bokeh.

you could even display these visualizations directly on a webpage using PyScript.

Leverage Python’s first-class support for data analysis and visualization to spruce up your web pages.

These films will leave you questioning humanity, but also wanting more.

Not all true crime is about hacking, slashing, and gore.

The key is not to spook your friends with over-the-top shenanigans.

Don’t let someone else take over your phone number.

If an AI can roast you, it can also prep you for emergencies.

Technology Explained

PC & Mobile