*CSV 包含虚假信息
Python on Kaggle w/ Pandas + Numpy
背景: 目标是导入/更新 Exchange 联系人。用户想要搜索学生的家长电子邮件。
输入(参见 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 评估条件
- 许多学生/监护人同姓且没有血缘关系
- 许多学生有超过 2 名监护人
- 许多学生不与一名或多名监护人同姓
- 有些学生是法定姓氏不同的兄弟姐妹
#1。我尝试过:使用 if 语句嵌套 for 循环
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.我尝试过的:使用
numpy.where()
: 嵌套 for 循环
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 functions, list comprehension
#我是
Pandas/Numpy/Python
的初学者。在练习时,我用 for 循环思考更容易,然后尝试将它们转换为列表理解。看来有很多方法可以解决这个问题。
这是我对你的要求的理解:
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': ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
}
# 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)