简体   繁体   中英

Run a function for each row and create a new Column Pandas Dataframe

I am using geoprapy to to get locations via a URL. I have a URL column for my DataFrame. I am attempting to run a pre-built Geograpy function on each URL and create a new column of the locations on the DataFrame. So, I have tried (from other questions):

hits['place'] = geograpy.get_place_context(url=hits.urls)

# and

hits['place'] = hits.apply(geograpy.get_place_context(url=hits.urls), axis=1))

# and

def getPlace(frame):
    urls = frame['urls']
    print(urls)
    frame['place'] = geograpy.get_place_context(url=urls)
    return frame

getPlace(hits)

Along with a few others. I keep getting

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Which I understand as that it is seeing URLs as a whole column and cannot run the function on the column? Doesn't really matter.

How can I run a function for every row in a dataframe and create a new column?

I expect my places to be a 'memory type object' I can reference later. I have part of this to work via:

for url in urls:
    place = (geograpy.get_place_context(url=url))
    region = place.country_regions

However, later in the code, the iterations causes it to fall apart.

pandas.apply function does exactly what you want, you just didn't pass the right argument. You can see in the documentation that you need to pass a function, not the result of the function call.

So, just pass geograpy.get_place_context to apply like this -

hits['place'] = hits['urls'].apply(geograpy.get_place_context, axis=1)

You should use .apply() over the urls column like:

hits['place'] = hits['urls'].apply(geograpy.get_place_context, axis=1)

This answer had helped find the distinction between different vectorization methods and their usage. Hope you find it useful too.

Edit: Since only one column is used to create another, .apply() over that column should work fine for you. .apply() is defined over a DataFrame as well as a Series .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM