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? 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
:
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
:
a = masterDF.author.str.split()
masterDF['email'] = masterDF.str[0] + '.' + masterDF.str[1] + '@gmail.com'
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.