[英]Create email addresses from first, last name
Goal: input a list of names and output list of corresponding email addresses using the structure 目标:使用该结构输入名称列表并输出相应的电子邮件地址列表
str(first_name) + '.' + str(last_name) + '@gmail.com'
the following function creates a list of randomly generated names... 下面的函数创建一个随机生成的名称列表...
import names
def fill_names(gender = 'female', n = n):
counter = 0
name_container = []
while counter < n:
name = names.get_full_name(gender = gender)
name_container.append(name)
counter += 1
return name_container
Now that I have the names, I will put them into a dataframe with a bunch of other dataseries that I will omit here... 现在我有了名字,我将它们放入一个数据框中,并在此省略其他一系列数据。
masterDF = pd.DataFrame(columns=['author', 'email')
masterDf.author = fill_names(n = n)
From here I am a bit unsure. 从这里我有点不确定。 Should i use the .split() method to split the first / last name in a for loop? 我应该使用.split()方法在for循环中拆分名字/姓氏吗? Something like (this is more psuedo code)... 有点像(这是更多的伪代码)...
for row in masterDF.author():
a = masterDF.author.split(' ')
email = a[0] + '.' + a[1] + '@gmail.com'
return email
Is there a better way to do this? 有一个更好的方法吗?
You can use str.replace
: 您可以使用str.replace
:
masterDF['email'] = masterDF.author.str.replace('\s+', '.') + '@gmail.com'
Sample: 样品:
masterDF = pd.DataFrame({'author':['name1 surname1','name2 surname2']})
masterDF['email'] = masterDF.author.str.replace('\s+', '.') + '@gmail.com'
print (masterDF)
author email
0 name1 surname1 name1.surname1@gmail.com
1 name2 surname2 name2.surname2@gmail.com
There is also possible use split
solution with str.split
and then join
: 也可以在str.split
使用split
解决方案,然后join
:
a = masterDF.author.str.split()
masterDF['email'] = masterDF.str[0] + '.' + masterDF.str[1] + '@gmail.com'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.