简体   繁体   中英

How to add 'www.' to the beginning of some dataframe values?

I have the data that contains domain names:

 url            var1
www.google.com   xsd
ebay.com         wer
www.amazon.com   xyz
microsoft.com    zyx
....

I need to add 'www.' to the domain names that don't have it at the beginning.

I have this code:

try: 
for domain in df['url']:
    if domain.startswith('www.'):
        next
    else: 
        domain = 'www.' + domain.astype(str)

except ConnectionResetError:
    print('Handle Exception')

The code is generating an error:

AttributeError: 'str' object has no attribute 'astype'

What am I doing wrong?

Don't use a loop when you can map() that function over all the rows.

def prefixWWW(url):
    return 'www.' + url if not url.startswith('www.') else url

df = df['url'].map(prefixWWW)

I'd do it this way:

In [235]: df.loc[~df.url.str.contains(r'^www\.'), 'url'] = \
              'www' + df.loc[~df.url.str.contains(r'^www\.'), 'url']

In [236]: df
Out[236]:
                url var1
0    www.google.com  xsd
1       wwwebay.com  wer
2    www.amazon.com  xyz
3  wwwmicrosoft.com  zyx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM