1

I have a Dataframe containing some Ids, here for example the "manufacturers_id". Below a portion of my dataframe

enter image description here

With an api request, I am able to get the Manufacturer from the Id. My function looks like below:

def get_manufacturer(m_id):
    url = "myapi/" + m_id + "?token=my_token"
    response = requests.get(url)
    manufacturer = json.loads(response.text)['name']
    return manufacturer

Used individually, I can retrieve manufacturer from an Id.

But now, I want to apply this function to the whole column of my dataframe.

df['manufacturers_id'] = df['manufacturers_id'].apply(get_manufacturer(df['manufacturers_id']))

But It give me an error: str is not callable. Or list indices must be integers or slices, not str, depending on my tries

Any help would be appreciated.

Tom Ron
  • 5,906
  • 3
  • 22
  • 38
Neandril
  • 122
  • 9

1 Answers1

4

Try

df['manufacturers_id'] = df['manufacturers_id'].apply(get_manufacturer)

Since the apply function used after a pandas Series indicates that entering each observation value into the function given in apply().

timgeb
  • 76,762
  • 20
  • 123
  • 145
Denny Chen
  • 489
  • 3
  • 8