python - Pandas: Convert List Comprehension to Use Apply -
i have pandas dataframe beautiful_soup column (it contains beautifulsoup object). want add column several html tags (e.g. number of img tags).
for instance, old code using list comprehension:
df['text_img_count'] = [len(x.find_all('img')) x in df['beautiful_soup']] but using apply should faster, wanted convert code.
i thinking of writing small function pass apply, like:
def get_imgs_count(): and i'd call this:
df['text_img_count'] = df['beautiful_soup'].apply(get_imgs_count) since i'm going doing bunch of html tags, don't want write ton of super similar functions. prefer writing like:
def get_tag_count(df, tag) and call this:
get_tag_count(df, 'img') but don't think can pass function arguments apply...
how might go converting list comprehension using apply?
thanks!
i use functools' partial application
from functools import partial def get_tag_count(bs, tag): return [len(x.find_all(tag)) x in bs] get_image_count = partial(get_tag_count, tag = 'img') df['text_img_count'] = df['beautiful_soup'].apply(get_image_count)
Comments
Post a Comment