简体   繁体   English

如何添加'www。' 到一些数据帧值的开头?

[英]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.' 我需要添加'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. 当你可以在所有行上map()函数时,不要使用循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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