Python: error in writing class -
i want write func , add class. use
import pandas pd import tldextract domain = [] df = pd.dataframe() df['urls'] = ['ru.vk.com', 'eng.facebook.com', 'ru.ya.ru'] urls = df.urls.values.tolist() class csv: def get_domain(self, list_url, list, df): self.list_url = list_url self.list = list self.df = df i, url in enumerate(list_url): get_domain = tldextract.extract(url) subdomain = get_domain[0] + '.' + get_domain[1] + '.' + get_domain[2] if subdomain.startswith('.'): subdomain = subdomain[1:] elif subdomain.endswith('.'): subdomain = subdomain[:-1] elif subdomain.startswith('www.'): subdomain = subdomain[4:] list.append(subdomain) df['subdomain'] = list df = csv() df.get_domain(urls, domain, df)
i try domain urls, error
attributeerror: csv instance has no attribute '__setitem__'
what should change?
you named variable use create csv
instance df
, same data frame item, making df
refer csv
object , not panda dataframe. then, when try call get domain
, df
refers class, has no way string indices, failing.
in short - change variable name in last 2 rows, like
csv_df = csv() csv_df.get_domain(urls, domain, df)
by way, it's not mistake, i'm pretty sure last row on get_domain
supposed be
self.df['subdomain'] = self.list
(same goes variables across function, since desire change class property , not given variable).
and shouldn't name variable list
or other reserved keyword. might cause issues.
Comments
Post a Comment