如何使用Python将学生ID列表从CSV文件转换为电子邮件地址?

huangapple go评论66阅读模式
英文:

How to convert student ID list to email address from CSV using Python?

问题

尝试使用Python将学生ID列表转换为电子邮件地址。我有一个将ID号码保存为CSV的列表,保存在一个名为“学生ID”的列中,我需要将其转换为特定的大学电子邮件地址格式。例如,我的ID是2128654,我需要将其转换为u2128654@live.university.ac.uk

对Python非常不熟悉,所以任何帮助将不胜感激。我刚学会如何使用dataframe = pd.read_csv读取数据库,所以目前这对我来说有点超纲。

我尝试了下面的方法,它可以可视化我的ID列表,但现在我需要添加一个新列,将转换后的电子邮件格式添加到该列,然后保存为一个新文件。

import pandas as pd
dataframe = pd.read_csv("/content/Student IDs.csv")
dataframe

CSV看起来像这样:

学生ID
123456789
012345678
901234567
890123456

请提供任何指导,不胜感激!

英文:

Trying to convert a list of student IDs to email addresses using Python. I have a list of ID numbers saved as a CSV in a single 'Student ID' column which I need to convert into given university email address formats. E.g. mine is 2128654' and I need to convert to u2128654@live.university.ac.uk.

Very new to Python so any help would be much appreciated please. Just learnt how to read databases using dataframe = pd.read_csv so this is a bit out of my league at this point.

I've tried the below which visualizes my list of IDs, but now need to add a new column, add the converted email formats to that column, then save as a new file.

import pandas as pd
dataframe = pd.read_csv("/content/Student IDs.csv")
dataframe

CSV looks like:

Student ID
123456789
012345678
901234567
890123456

Any guidance much appreciated please!

答案1

得分: 2

尝试使用df.astype()来连接

df['Email'] = 'u' + df['Student ID'].astype(str) + '@live.university.ac.uk'
print(df) 


  Student ID                             Email
0  123456789  u123456789@live.university.ac.uk
1  012345678  u012345678@live.university.ac.uk
2  901234567  u901234567@live.university.ac.uk
3  890123456  u890123456@live.university.ac.uk

要保存一个新文件:

df.to_csv('/content/output.csv', index=False)
英文:

Try this approach using df.astype() to concatenate

df['Email'] = 'u' + df['Student ID'].astype(str) + '@live.university.ac.uk'
print(df) 


  Student ID                             Email
0  123456789  u123456789@live.university.ac.uk
1  012345678  u012345678@live.university.ac.uk
2  901234567  u901234567@live.university.ac.uk
3  890123456  u890123456@live.university.ac.uk

To save a new file:

df.to_csv('/content/output.csv', index=False)

答案2

得分: 1

你会希望将你的 csv 作为 strings 读取,因为前导的零将被删除。然后使用 apply 进行以下字符串连接 u + Student# + @live.university.ac.uk

import pandas as pd
dataframe = pd.read_csv("/content/Student IDs.csv", dtype=str)
dataframe["Student Emails"] = test["Student ID"].apply(lambda x: "u" + x + "@live.university.ac.uk")
英文:

You'd want to read your csv as strings since leading 0s will be removed. Then use apply to do the following string concatenation u + Student# + @live.university.ac.uk:

import pandas as pd
dataframe = pd.read_csv("/content/Student IDs.csv", dtype=str)
dataframe["Student Emails"] = test["Student ID"].apply(lambda x: "u" +x + "@live.university.ac.uk")

huangapple
  • 本文由 发表于 2023年3月12日 08:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定