avatarDiksha Madan

Summary

The web content provides a step-by-step guide on how to retrieve stock market data from an API, convert it into a pandas DataFrame, and save it as a CSV file using Python.

Abstract

The article titled "How to Fetch Data From an API and Save as a CSV File using Python" outlines a process for extracting stock market data from the Alpha Vantage API, transforming the JSON response into a structured pandas DataFrame, and subsequently exporting this data to a CSV file. It emphasizes the necessity of converting JSON data into a DataFrame to facilitate further data manipulation and analysis. The author, Diksha Madan, demonstrates the use of Python libraries such as requests for making HTTP requests, json for parsing JSON data, and pandas for creating and manipulating DataFrames. The tutorial includes code snippets for each step, from sending an API request to saving the CSV file with dynamic naming based on the stock symbol. The article also provides insights into handling nested JSON structures and setting appropriate row headers in the DataFrame.

Opinions

  • The author considers the process of fetching data from an API and converting it to a CSV file to be straightforward and accessible, even for those using the API without an API key.
  • Diksha Madan suggests that the requests library simplifies the process of sending HTTP requests to APIs.
  • The article implies that organizing data into a pandas DataFrame is a crucial step before converting it to a CSV format, highlighting the importance of data frames in data analysis workflows.
  • The author's approach to dynamically naming the CSV files based on the stock symbol indicates a preference for automated and scalable data processing solutions.
  • By providing a comprehensive example, including error checking through status codes, the author conveys a best practice approach to API data retrieval and handling.

How to Fetch Data From an API and Save as a CSV File using Python

Photo by Ferenc Almasi on Unsplash

There are times in a project when we need to extract data from an API. The data that we get from an API is a JSON, in most cases, a nested JSON. If we need to do any further transformation or exploration of the data, we will need to convert it into a pandas data frame. Only after the data is of the type data frame, we can convert it into a CSV. So let’s see how we can convert a JSON into a data frame and then in turn into a CSV file.

The API I am using is around the stock market. Here is the URL for the documentation: https://www.alphavantage.co/documentation/

It is super easy to get your free API key. You can even use the API even without the API key as an example.

Install libraries

import pandas as pd
import requests
import json
import csv

Send a request to the API

We now need to send a request to the API. The requests library is used for that. This library allows you to send HTTP requests easily.

In this code snippet, I am using TIME_SERIES_DAILY function from the API, for IBM. The API key is ‘demo’.

response = requests.get(f'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo')

Get a response from the API

We need to check where we can send a request to the API successfully. For that, we need to check the status code. If it’s 200 — we are good!

print(response.status_code)
status code: 200

The response is 200, which means the request has succeeded.

Get the data in DataFrame

Since we can send a request to the API, we now need to get data from it.

data = response.text

The response.text initializes the entire data coming out from the API to the data variable as a string type.

But we need data in JSON, and consequently in a data frame. To have it changed to a JSON type, we need to use a function.

Load the data in JSON

json_data = json.loads(data)

This loads the string-type data to JSON-type data.

Now our data is in JSON and is ready to be converted to a data frame.

Convert JSON data into a data frame

If you look at the JSON data carefully, you’ll see that the first key-value pair is just metadata, which we don’t need in a data frame. We directly need values from the ‘Time Series (Daily)’ key and need to ignore the ‘Meta Data’ key.

JSON data

To do this we need to run a loop for all the values of the ‘Time Series (Daily)’ key. Now if you look carefully, we also have nested JSON, with the key as date and values as open, high etc.

For this, we’ll create two arrays, one for values and the other for headers. In the loop, the i variable is the date, which we will append in the headers array. For values, we need to get the value of the key, i. You can print the arrays or variables and see the data — this will be even clearer.

values = []
headers = []

for i in json_data['Time Series (Daily)']:
  headers.append(i)
  values.append(json_data['Time Series (Daily)'][i])

Once we have our headers and values arrays, we will create a data frame from the values array. The values array is an array of dictionaries, so when we use the DataFrame function of the pandas library, it automatically makes the key row headers and values and data frame values.

output of values array
df = pd.DataFrame(values)

This will directly convert the values array into a data frame.

JSON converted into data frame

Voila!

Now, we also have headers array which consists of dates. In the above screenshot, as you can see, we have the row headers as an integer number. We need to change it into the dates from our headers array.

To do that, there is the function set_axis(), which sets the row headers.

df.set_axis(headers, axis=0, inplace=True)
Row headers are date now!

And that’s it! Now we can save this data frame as a CSV file.

Data frame to CSV

Since we were getting the stock name in the ‘Meta Data’ key, I thought of saving the file name as dynamic to the stock name.

Meta Data
name = json_data['Meta Data']['2. Symbol']
df.to_csv(f'stock_data_{name}.csv')

This will return the CSV file as stock_data_IBM.csv

Entire Code

I am sharing the code that will save the data into different CSV for each company mentioned in the list automatically, in a single run. You can add or remove the company symbols in the company array, as per your wish.

Note: To get data for multiple companies, you need to get your API key.

import pandas as pd
import requests
import json
import csv

company = ['AAPL', 'IBM', 'AMZN', 'MSFT', 'TSLA', 'GOOGL', 'META', 'LYFT', 'INTC', 'JBLU']


#function to get the company name 
def get_name(json):
    name = json_data['Meta Data']['2. Symbol']
    return name

#function to convert data into csv
def get_csv(json, name):
    values = []
    headers = []
    for i in json_data['Time Series (Daily)']:
        headers.append(i)
        values.append(json_data['Time Series (Daily)'][i])
    df = pd.DataFrame(values)
    df.set_axis(headers, axis=0, inplace=True)
    df.to_csv(f'stock_data_{get_name(json)}.csv')



#loop for each company
for i in company:
    response = requests.get(f'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={i}&apikey=<your_api_key>')
    print(response.status_code)
    data = response.text
    json_data = json.loads(data)
    get_csv(json_data, get_name(json_data))

There you have it! Multiple files, each with a separate name!

Contact me at [email protected] or https://www.linkedin.com/in/diksha-madan/

Thanks!

Csv
Json
API
Python
Json Api
Recommended from ReadMedium