1

there are bunch of rest api request, where each request have similiar data. Once I call the url and parse the data that I want, I want to create pandas data frame, create one big data frame after I am done with all of the requests. So data string below should be appended to a data frame:

   #this code is to call each url and get the data 
    hyperId=['hyper1', 'hyper2','hyper3']
    #creating empty data frame with column names
    df1=pd.DataFrame(columns = ['id','servername', 'modelname'

    for id in hyperId1:
        hyperUrl="http://testabc"+id
        resp = requests.get(hyperurl)
        data1=id+","+resp['servername']+","+resp['model']

        #output of each request
        print(data1)
        hyper101,serverabc,proliant

I need to append data1 to data frame called df1 as below

df1 = pd.read_csv(io.StringIO(data1))
df1=df1.append(data)

I am very new to python and pandas. When I run this it says empty data frame with everything appended to the column including column names and actual data. Any help would be appreciated.

2
  • Create an empty list, make data1 a list and append it to the empty list. You would then end up with a list of lists which you could use with pd.DataFrame to create a data frame. Commented Jul 28, 2021 at 16:05
  • Just read all the data from the API into a list, then finally create the dataframe out of it using df = pd.DataFrame(dataList, columns= ['id','servername', 'modelname']) Commented Jul 28, 2021 at 16:05

1 Answer 1

1

Don't create any dataframe upfront, and also don't convert and concatenate the string values (since it may have performance impact for a large data). Just create an empty list to hold each rows (as a list), and append each of the list in a loop, then finally create the dataframe, once you have required list of lists.

import  requests
# this code is to call each url and get the data 
hyperId = ['hyper1', 'hyper2', 'hyper3']
# Don't create any Data Frame
# df1 = pd.DataFrame(columns=['id', 'servername', 'modelname'])
dataList = []
for id in hyperId:
    hyperUrl = "http://testabc" + id
    resp = requests.get(hyperUrl)
    # data1 = id + "," + resp['servername'] + "," + resp['model']
    dataList.append([id, resp['servername'], resp['model']])

df = pd.DataFrame(dataList, columns=['id', 'servername', 'modelname'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.