0

I've got a dataframe like below with 66,000 rows..

Client Nodes
Client A [987673, 932132, 3132131, 3123443, ...]
Client B [4324234, 56345, 5435345, 5345345, ...]

What I need to do is run the below function on the list within each row and then put the result in a new column.

I've tried using the .apply function but not sure how to loop it through the list

RouteNodeLL = []

for node in route_nodes:
    response_xml = requests.get(f'https://api.openstreetmap.org/api/0.6/node/{node}')
    response_xml_as_string = response_xml.content
    responseXml = ET.fromstring(response_xml_as_string)
    for child in responseXml.iter('node'):
        RouteNodeLL.append((float(child.attrib['lat']), float(child.attrib['lon'])))
DRobins
  • 61
  • 1
  • 3

1 Answers1

1

Assuming that the code in your snippet is actually enclosed in a function you should be able to use .apply as follows.

If that I have a DataFrame

df = pd.DataFrame({
    'Client': ['Client A', 'Client B'],
    'Nodes': [[987673, 932132, 3132131, 3123443], [4324234, 56345, 5435345, 5345345]]
})

And I want to compute some value based on the list in each row (trivially the sum), I can define a function to compute the value

def my_fun(entry_list):
  return sum(entry_list)

and apply the function to the column containing the list to create the new desired column.

df['Result'] = df['Nodes'].apply(my_fun)

If this doesn't work please provide more context, like the actual function.

azz
  • 11
  • 2
  • This is my intention, but when I try the above i'm getting an "TypeError: ("'float' object is not iterable" error as it's not seeing the nodes as a list – DRobins Jul 07 '21 at 15:54
  • I need more context here, can you send the error trace? I would useful to know which line is triggering the error. Also is the code above the whole implementation of your function? I don't see the `def` and the `return`. – azz Jul 08 '21 at 09:44