Reputation: 2231
I have a large DataFrame that looks consists of a list of game titles, which often contain their console.
title
1 Nier Automata (ps4)
2 Halo 5 Xbox One
I want to automatically assign it to a new column, category, which contains the names of the console.
title category
1 Nier Automata (PS4) PS4
2 Halo 5 Xbox One Xbox One
I thought this would be a perfect fit for an .apply operation and wrote the following code:
console_list = ['PS4', 'Xbox One', 'PC', 'PS3', 'PS2', 'Xbox 360', '3DS']
df['category'] = df['title'].apply(lambda x: y for y in console_list if y in x)
It throws the following error:
File "scraper.py", line 153, in <module>
df['auto_categorie'] = df['titel'].apply(lambda x: y for y in console_list if y in x)
File "/venv/lib/python3.6/site-packages/pandas/core/series.py", line 2551, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src/inference.pyx", line 1521, in pandas._libs.lib.map_infer
TypeError: 'generator' object is not callable
But I though I was not calling the list object itself, but its contents (y), that anyone have an idea what I am doing wrong?
Upvotes: 1
Views: 1201
Reputation: 27879
Due to case inconsistency I would suggest you to use upper()
:
df['category'] = df['title'].apply(lambda x: list([y for y in console_list if y.upper() in x.upper()]))
Upvotes: 1
Reputation: 863176
It seems you need list comprehension
:
df['category'] = df['title'].apply(lambda x: list([y for y in console_list if y in x]))
Another solution with str.findall
:
df['category'] = df['title'].str.findall('|'.join(console_list))
print (df)
title category
1 Nier Automata (PS4) [PS4]
2 Halo 5 Xbox One [Xbox One]
Upvotes: 3