Aug-11-2019, 11:02 AM
Hi there,
I'm starting learning Python to be able to replace excel and be more efficient. I have a dataframe with a column called 'Source' which contains a list of URLs. I want to create a new column called 'Category' which will have the category of the URL. The category will be based on the strings inside the URL.
When I look for this type of URL categorisation, it seems that most of the people use more difficult and advance systems. Is it not possible to use a if function for this purpose?
I have tried this code but it seems that the syntax is not right. Any advice on where to look to find out simple ways to classify URLs?
I'm starting learning Python to be able to replace excel and be more efficient. I have a dataframe with a column called 'Source' which contains a list of URLs. I want to create a new column called 'Category' which will have the category of the URL. The category will be based on the strings inside the URL.
When I look for this type of URL categorisation, it seems that most of the people use more difficult and advance systems. Is it not possible to use a if function for this purpose?
I have tried this code but it seems that the syntax is not right. Any advice on where to look to find out simple ways to classify URLs?
category = def categories (df) = { if (df['Source'].str.contains("/string1",regex=True)): 'Category 1', elif (df['Source'].str.contains("/string2",regex=True)): 'Category 2', else: other } df['Category']= category df.head()Many thanks!