3
\$\begingroup\$

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("pandas", na=False), 'Name'] = "PANDAS"
df.loc[df['Name'].str.contains("python|pitone", na=False), 'Name'] = "PYTHON"
print(df.head())
\$\endgroup\$
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You should probably use df['URL'].str.split('.').str[-2].str.upper() instead of a list comprehension for some more speed. \$\endgroup\$ – Graipher Jun 26 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.