Api To Csv File Conversion In Machine Learning

Api To Csv File Conversion In Machine Learning

API TO CSV FILE IN 6 STEPS IN MACHINE LEARNING

hey folks hope you are all doing great so from this article i will try to introduce you all how to fetch data from api in machine learning .

Prerequisite for this article

  1. you know basic about api
  2. how api works
  3. api key to fetch data
  4. basis about json , how json is useful

for this article i wil be using tmdb site which show popular movies from hollywood,bollywood and other all around the world.

STEP 1: import libraries

import pandas as pd
import requests

import_library.PNG

STEP 2: SENDING REQUEST FOR SITE TO FETCH DATA USING API KEY

response=requests.get('https://api.themoviedb.org/3/movie/top_rated?api_key=&language=en-US&page=1')

fetch_using_api_key.PNG

STEP 3: CONVERT JSON INTO READABLE DATA IN ML

data=pd.DataFrame(response.json()['results'])[['id','title','popularity','overview','vote_average','vote_count','release_date']]
data

JSONTOMLDATA.PNG

STEP 4 : CREATE ANOTHER DUMPY DATA FRAME TO GET WHOLE DATA

df=pd.DataFrame()

STEP 5: FETCH ALL PAGE DATA FROM API

for i in range(1,501):
  response=requests.get('https://api.themoviedb.org/3/movie/top_rated?api_key=c4240d67d1a5666f710c870a14552172&language=en-US&page={}'.format(i))
  data=pd.DataFrame(response.json()['results'])[['id','title','popularity','overview','vote_average','vote_count','release_date']]
  df=df.append(data,ignore_index=True)

ALL DATA.PNG

STEP 6 : CONVERT API DATA TO CSV FILE

df.to_csv('top_rated_movies.csv')

image.png after writing this code you will get .csv file in your folder either in google collab or in jupyternotebook from their you can download it and upload it to the kaggle and boost your kaggle profile .