我有包含域名的数据:
url var1
www.google.com xsd
ebay.com wer
www.amazon.com xyz
microsoft.com zyx
....
我需要添加“www”。到开头没有它的域名。
我有这个代码:
try:
for domain in df['url']:
if domain.startswith('www.'):
next
else:
domain = 'www.' + domain.astype(str)
except ConnectionResetError:
print('Handle Exception')
代码生成错误:
AttributeError: 'str' object has no attribute 'astype'
我做错了什么?
最佳答案
当您可以在所有行上使用 map()
函数时,请勿使用循环。
def prefixWWW(url):
return 'www.' + url if not url.startswith('www.') else url
df = df['url'].map(prefixWWW)
关于python - 如何将 'www.' 添加到某些数据帧值的开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42753385/