Home > database >  download csv data from multiple URLs and combine to a single dataframe
download csv data from multiple URLs and combine to a single dataframe

Time:10-21

I'm new to Python and trying to combine multiple 1-row DataFrames after downloading each via URL. I've been trying to use pandas pd.concat() without success. So far I get the individual DataFrames correctly, but combining them is not working using the following script:

import pandas as pd
import time
from time import sleep
import numpy as np
import glob
import pathlib as pl2

count = 0

with open('tickertest12.txt', 'r') as my_file:
    newline_break = ""
    for readline in my_file:
        line_strip = readline.strip()
        newline_break  = line_strip

#start the counter to pause the loop after x lines
        
        count  =1
        
        try:
        
            urls = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol="   line_strip   "&apikey=demo&datatype=csv"

            # Using pandas.concat() to append a row
                    
            #print(url)
        
            dfs = pd.read_csv(urls,header=0)

            big_df = pd.concat(dfs,ignore_index=True)
            print("this is big_df")
            print(dfs)
                    
        
                            
        except:
            print("Invalid Symbol",line_strip)


        if count == 3:
            sleep(0)
            print("pausing for 2 secs")
            count = 0
            

The .txt file is a list of ticker symbols: IBM, etc.

Assistance on what is missing or incorrect with the above would be much appreciated.

CodePudding user response:

you need two DFs for concat to combine

change as follows

# define the empty dataframe prior to the line '`With open`', below `count=0`
big_df=pd.DataFrame()
big_df = pd.concat([big_df, dfs],ignore_index=True)
  • Related