所以,我想制作一个数据框,其中包含预约兽医所需的信息。
Data Frame Content :
> Pet Owner's Name
> Pet Information
> Pet Name
> Pet Breed
> Pet Age
基于这些数据框,我想使用宠物主人的名字作为约会的名称(约会:Natasha),这样我就可以仅通过宠物显示约会信息(如宠物名称、品种和年龄)业主姓名。
但是,我对如何在数据框中排列所有这些内容感到困惑。
我试过这个:
df_appt = {
"Appointment" : [], "Pet Information" : {
"Pet Name" : [],
"Pet Breed" : [],
"Pet Age" : []
}
}
但是当我尝试使用 pandas 将其作为数据框上传时,它显示了输出
ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.
并且我无法将其导入为 Excel 文件。
但是,我认为我的桌子不对(?)
是否有更好或更有效的方法将它们排列在表格中? 并将其排列在数据框上?
注释:我不能使用SQL,这个数据框可以保存多个预约数据(所以它不仅仅适用于一个宠物主人)
我建议使用“平面”数据框并作为索引设置“约会”。
例如,您有这个数据框:
Appointment Pet Name Pet Breed Pet Age
0 John Bob Dog 3
1 John Flip Rabbit 9
2 Natasha Flop Bird 1
然后你可以这样做:
df = df.set_index("Appointment")
print(df)
Pet Name Pet Breed Pet Age
Appointment
John Bob Dog 3
John Flip Rabbit 9
Natasha Flop Bird 1
要选择约翰的所有宠物,请使用
df.loc
:
print(df.loc["John"])
打印:
Pet Name Pet Breed Pet Age
Appointment
John Bob Dog 3
John Flip Rabbit 9