条件拼接用于Exchange联系人导入
*CSV包含虚假信息
在Kaggle上用Python和Pandas + Numpy
背景: 目标是导入或更新交流联系人。用户想通过他们的学生来查找家长的电子邮件。
输入(见CSV截图):
- 'Student ID' column (7-digit number) - 'Parent ID' column (7-digit number) - 'Student First Name' column (str) - 'Student Last Name' column (str) - 'Parent First Name' column (str) - 'Parent Last Name' column (str)
期望输出(见CSV截图):
- 'Contacts' column *'Contacts' column follows this pattern: - One Student: "studentLastName, studentFirstName (parentLastName, parentFirstName)" - Multiple Students: "studentLastName, student1FirstName, student2FirstName, & student3FirstName (parentLastName,> parentFirstName)"
假设:
- 使用学生/家长的ID来评估条件
- 许多学生/监护人有相同的姓氏,但并没有关系
- 许多学生有超过两个监护人
- 许多学生与一个或多个监护人没有相同的姓氏
- 一些学生是兄弟姐妹,但法律上的姓氏不同
#1. 我尝试过的:用嵌套的for循环和if语句
for i in range(14):
for j in range(14):
if contacts.iloc[i]['parentID'] == contacts.iloc[j]['parentID'] and i!=j:
hold = [contacts.iloc[i]['studentFirstName'], contacts.iloc[j]['studentFirstName']]
print(hold)
#我期待的结果:兄弟姐妹的学生名字列表,前提是对应的parentIDs
在不同的行中匹配
#示例输出:
[[Uganda, Tiramisu], [Uganda, Tiramisu], [Ethernet, Persimmon], [Ethernet, Niagara], [Persimmon, Niagara]]
#我对DataFrame还很陌生。我想把一切都当作列表来处理。
#2. 我尝试过的:用嵌套的for循环和numpy.where()
:
for i in range(14):
for j in range(14):
print(i, j)
print(contacts.iloc[i]['parentID'], contacts.iloc[j]['parentID'])
np.where(contacts.iloc[i]['parentID'] == contacts.iloc[j]['parentID'],
print(contacts.iloc[i]['studentFirstName'] + ' and ' + contacts.iloc[j]['studentFirstName']), print('1'))
#我期待的结果:student1FirstName
和student2FirstName
在parentIDs
匹配时输出,否则输出1
#我不太理解广播的概念,感觉这和打印结果有关系,导致出错。
#考虑其他方法:pd.agg(), pd.groupby(), lambda函数, 列表推导
#我对Pandas/Numpy/Python
还是个新手。在练习时,我觉得用for循环更容易理解,然后再尝试把它们转换成列表推导。似乎有很多方法可以解决这个问题。
1 个回答
0
这是我对你所问问题的理解:
import pandas as pd
data = {
'Student ID': [1, 1, 4, 4, 4, 5, 5, 5, 9, 9, 10, 10, 22, 22],
'Parent ID': [2, 3, 6, 7, 8, 6, 7, 8, 3, 4, 3, 4, 3, 4],
'Student First Name': ['Native', 'Native', 'Uganda', 'Uganda','Uganda','Tiramisu', 'Tiramisu', 'Tiramisu', 'Ethernet', 'Ethernet', 'Persimon', 'Persimon', 'Niagra', 'Niagra'],
'Student Last Name': ['Americans', 'Americans', 'Socrates', 'Socrates','Socrates','Socrates', 'Socrates', 'Socrates', 'Regression', 'Regression', 'Regression', 'Regression', 'Regression', 'Regression'],
'Parent First Name': ['Femur', 'Gigantic', 'USBC', 'Lei','Expo','USBC', 'Lei', 'Expo', 'Monitor', 'Isopropyl', 'Monitor', 'Isopropyl', 'Monitor', 'Isopropyl'],
'Parent Last Name': ['Americans', 'Peanut', 'Socrates', 'Socrates','Socrates','Socrates', 'Socrates', 'Socrates', 'Chocolate Chips', 'Bits n Bits', 'Chocolate Chips', 'Bits n Bits', 'Chocolate Chips', 'Bits n Bits'],
'parent_email': ['Femur.A@email.com', 'Gigantic.P@email.com', 'USBC.S@email.com', 'Lei.S@email.com', 'Expo.S@email.com', 'USBC.S@email.com', 'Lei.S@email.com', 'Expo.S@email.com', 'Monitor.C@email.com', 'Isopropyl.B@email.com', 'Monitor.C@email.com', 'Isopropyl.B@email.com', 'Monitor.C@email.com', 'Isopropyl.B@email.com']
}
# Create DataFrame
df = pd.DataFrame(data)
print(df)
# Group parent emails by students
grouped_emails = df.groupby(['Student ID', 'Student First Name', 'Student Last Name'])['parent_email'].apply(list).reset_index()
grouped_emails.to_excel("grouped_emails.xlsx", index=False)
# Display grouped emails
print(grouped_emails)