Home > Mobile >  In Python, How do you use a loop to create a dataframe?
In Python, How do you use a loop to create a dataframe?

Time:01-21

I recently pulled data from youtube API, and I'm trying to create a data frame using that information.

When I loop through each item with the "print" function, I get 25 rows output for each variable (which is what I want in the data frame I create).

How can I create a new data frame that contains 25 rows using this information instead of just 1 line in the data frame?

When I loop through each item like this:

df = pd.DataFrame(columns = ['video_title', 'video_id', 'date_created'])

#For Loop to Create columns for DataFrame    

x=0

while x < len(response['items']):
    video_title= response['items'][x]['snippet']['title']
    video_id= response['items'][x]['id']['videoId']
    date_created= response['items'][x]['snippet']['publishedAt']
    x=x 1

#print(video_title, video_id)
df = df.append({'video_title': video_title,'video_id': video_id,
            'date_created': date_created}, ignore_index=True)

=========ANSWER UPDATE========== THANK YOU TO EVERYONE THAT GAVE INPUT !!!

The code that created the Dataframe was:

import pandas as pd

x=0
video_title = []
video_id = []
date_created = []

while x < len(response['items']):
    video_title.append (response['items'][x]['snippet'] 
    ['title'])
    video_id.append (response['items'][x]['id']['videoId'])
    date_created.append (response['items'][x]['snippet']. 
    ['publishedAt'])
    x=x 1

#print(video_title, video_id)
df = pd.DataFrame({'video_title': video_title,'video_id': 
video_id, 'date_created': date_created})

CodePudding user response:

Based on what I know about youtube APIs return objects, the values of 'title' , 'videoId' and 'publishedAt' are strings. A strategy of making a single df from these strings are:

  1. Store these strings in a list. So you will have three lists.
  2. Convert the lists into a df

You will get a df with x rows, based on x values that are retrieved.

Example:

import pandas as pd

x=0
video_title = []
video_id = []
date_created = []

while x < len(response['items']):
    video_title.append (response['items'][x]['snippet']['title'])
    video_id.append (response['items'][x]['id']['videoId'])
    date_created.append (response['items'][x]['snippet']['publishedAt'])
    x=x 1

#print(video_title, video_id)
df = pd.DataFrame({'video_title': video_title,'video_id': 
video_id, 'date_created': date_created})
  •  Tags:  
  • Related