Running all rows in a column through a Python function

Viewed 31

I have a dataframe in Python called df_test_geo that looks as such:

{'userid': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7}, 'text_string': {0: 'I live in Miami and work in software', 1: 'Chicago, IL', 2: 'Dog Mom in Cincinnati', 3: 'Accountant at @EY/Baltimore', 4: 'World traveler but I call Atlanta home', 5: 'Lover of dogs!', 6: 'Working in Orlando. From Korea.'}}

I am then using the following package: from geotext import GeoText

If I run the following:

places = GeoText(df_test_geo.text_string[0])

places.cities

It correctly returns:

['Miami']

But what if I want to capture all the responses by creating a new column called location where it stores the responses from the above code, but for all rows in the text_string column?

So the location output would look like this (in order): Miami, Chicago, Cincinnati, Baltimore, Atlanta, '', Orlando

The spot between Atlanta and Orlando is empty because there is no city in text_string so the package won't return anything.

When I run:

places = GeoText(df_test_geo.text_string)

places.cities

It returns the following error: TypeError: expected string or bytes-like object

0 Answers
Related