1

When I save the data to csv file only records with id 103 are saved and records having ids 101 and 102 are not available.

import pandas as pd
import requests as rq

for vehicleList in range(101, 103):
    vehicleList = vehicleList + 1
    x = str(vehicleList)

    r = rq.get('https://api.tfl.gov.uk/vehicle/' + x + '/arrivals')
    r = r.text
    df = pd.read_json(r)

    df.to_csv('filename.csv')
3
  • can you add the url complet? I mean in which case x ....101...102...? Commented Dec 11, 2019 at 22:32
  • 2
    First of all, fix your indentation. Second, it looks like you override your file after each loop. Commented Dec 11, 2019 at 22:32
  • Variable and function names should follow the lower_case_with_underscores style. Commented Dec 12, 2019 at 1:29

1 Answer 1

3

You need to concatenate the data before saving. For example:

import pandas as pd
import requests as rq

temp = pd.DataFrame()
df = pd.DataFrame()

for vehicleList in range(101, 103):
    vehicleList = vehicleList + 1
    x = str(vehicleList)
    r = rq.get('https://api.tfl.gov.uk/vehicle/' + x + '/arrivals')
    r = r.text
    temp = pd.read_json(r)

    # some identifier here  
    temp['Type'] = x

    # concat
    df = pd.concat([df, temp]).reset_index(drop=True)

df.to_csv('filename.csv')

You assign to a temporary DataFrame each loop temp and then add a unique id to denote the group temp['Type'] and then concatenate to df. Once the loop is complete you save df with all iterations included.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you vey much ,I'm new to python so I'm missing so much.

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.