sabr ine
sabr ine

Reputation: 17

how to solve " arrays must all be same length" error

I am trying to extract some YouTube comments with details, and when I wanted to store the dataframe from my dictionary like this

 #######################################################################
  #----Store comments in CSV files----
 #######################################################################
 output_dict = {
    'Channel': channel_pop,
    'Video Title': video_title_pop,
    'Video Description': video_desc_pop,
    'Video ID': video_id_pop,
    'Comment': comments_pop,
    'Comment ID': comment_id_pop,
    'Likes': like_count_pop,
    'publishedAt': published_at_pop,
    'authorChannelId':authorChannelId_pop,
    }code here

output_df = pd.DataFrame(output_dict, columns = output_dict.keys())

I faced this error "ValueError: arrays must all be same length". can someone propose to me a solution?? Any help would be much appreciated.

Upvotes: 0

Views: 113

Answers (1)

pakpe
pakpe

Reputation: 5479

Each value in the output_dict must be a list before it can be converted to a dataframe. You can covert your dictionary to a dictionary of lists, then convert to a dataframe as follows:

for key, value in output_dict.items():
    output_dict[key] = [value]
    
output_df = pd.DataFrame(output_dict, columns = output_dict.keys())

Upvotes: 1

Related Questions