1

I'm trying to create a new df column while iterating over a df.series and checking if the string contains.

some_list = ['Nashville', 'Oak Ridge']
data.COLUMN
...
'Nashville is a great city'
'Oak Ridge has a research center'

for city in some_list:
    for row in data.COLUMN:
        if city in row:
            data['CITY'] = city

basically, I want to create a city column with the city from the list if the city is contained in the iteration

redeemefy
  • 4,521
  • 6
  • 36
  • 51
  • See the linked dupe above, and [this answer](https://stackoverflow.com/a/55554709/4909087) as well. – cs95 Apr 18 '19 at 01:21
  • He want to create a new column, not boolean indexing with `.isin`. Not sure if this a dup of the particular answer you marked it with @cs95 – Erfan Apr 18 '19 at 01:25
  • @Erfan `if city in row` seemed to give it away. Looks like they want to set values based on `isin`, which is only a step away from here :) – cs95 Apr 18 '19 at 01:26
  • It is not a duplicate. I know that `data.COLUMN.str.contains('some string')` will return a series with booleans. I don't want that. I want to create a new column based on that condition instead. – redeemefy Apr 18 '19 at 01:29
  • 1
    I think he was going for something like this: `df['city'] = df.apply(lambda r: ", ".join([city for city in some_list if city in r['statement']]), axis=1)` – Cohan Apr 18 '19 at 01:33
  • The above comment led me to my solution! `def insert_city(x): for city in cities: if city.lower() in str(x['STATION_NAME']).lower(): return city` `data['CITY'] = data.apply(insert_city, axis=1)` – redeemefy Apr 18 '19 at 02:08

0 Answers0