Home > front end >  Adding a column value from variable in Pandas dataframe
Adding a column value from variable in Pandas dataframe

Time:08-24

I have a piece of python code that is part of a bigger code that gets data using an API. I have created several for loops, but the heart of the for loop is:

    for page in offets:
        base_url = base_url   id   str(page)
        response = requests.get(base_url, auth=auth))
        output_from_api = response.json()
        raw_data.append(output_from_api)
        data = pd.DataFrame(output_from_api['elements'])
        dataframes_contact.append(data)

this works for multiple id's however its difficult to determine which dataset belongs to which id. I think the best way would be to add a column that appends the id to a column, however I have been unsuccessful.

I have tried this but does not work:

data.apply(id, axis=1)

this is the output I would like. the id column will be filled in as the for loop fills data. desired output

using Python 3.9, pandas==1.4.3

any help / suggestions will be really appreciated

CodePudding user response:

Instead of using apply I think you should just set the id column like so:

data['id'] = id
  • Related