0

Fetching data from API with for loop, but only last row is showing. If i put print statement instead of d=, I get all records for some reason. How to populate a Dataframe with all values?

I tried with for loop and with append but keep getting wrong results

for x in elements:
    url = "https://my/api/v2/item/" + str(x[number"]) + "/"
    get_data = requests.get(url)
    get_data_json = get_data.json()
    d = {'id': [x["enumber"]], 
         'name': [x["value1"]["name"]],
         'adress': [value2["adress"]],
         'stats': [get_data_json["stats"][5]["rating"]]
        }
df = pd.DataFrame(data=d)
df.head()

Result:

id name  order  adress  rating 

Only last row is showing, probably because it's overwriting until it comes to last element. Should I put another for loop somewhere or there is some obvious solution that I cannot see?

2
  • You need to look into df.append just create the dataframe once before adding all the data then append the new data Commented Feb 16, 2019 at 0:26
  • @Jaba generally that is not the way to go. Append the results into a list and then use that list to build the data-frame. .append with a data-frame is needlessly inefficient Commented Feb 16, 2019 at 0:27

1 Answer 1

3

Put all your data into a list of dictionaries, then convert to a dataframe at the very end

At the top of your code write:

all_data = []

Then in your loop, after d = {...}, write

all_data.append(d)

Finally at the end (after the loop has finished)

df = pd.DataFrame(all_data)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, it's populated now, just all values are in [], eg [3] [2] [100] , how to remove that or it doesn't matter?
is it because I used these parentheses in [x["entry_number"]] etc?
@Mr.Mister. It does matter, you should use x["entry_number"] instead of [x["entry_number"]], there's no need for the outside brackets []

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.