Creating names from URLs I am creating a NAME column in a DataFrame and set its value based on substrings contained in another column.
Is there a more efficient way to do this?
import pandas as pd
df = pd.DataFrame([['www.pandas.org','low'], ['www.python.org','high']],
     columns=['URL','speed'])
print(df.head())


df['Name'] = df['URL']  
print(df.head())

#set Name based on substring in URL
df.loc[df['Name'].str.contains(&quot;pandas&quot;, na=False), 'Name'] = &quot;PANDAS&quot;
df.loc[df['Name'].str.contains(&quot;python|pitone&quot;, na=False), 'Name'] = &quot;PYTHON&quot;
print(df.head())
Yes. Try to automate converting a URL to a name, instead of hardcoding the mapping. With only two URLs, your approach is doable, but as soon as you have to handle lots of different URLs, it will quickly become very painful.
Also, avoid first copying a column and then replacing every item in it. Rather, construct the column with the right contents directly. Here is an example:
df['Name'] = [url.split('.')[-2].upper() for url in df['URL']]

This will fail for proper URLs like https://www.pandas.org/index.html. If you want to handle that automatically, you'll have to properly parse them, for example using urllib.parse.