我有一个数据框,我想从中获取某些值以输入新字典。我想重命名数据框中的某些列,并使其在字典中也成为键。如何从头开始输入数据框中的列值作为字典中的值,从头开始构建动态字典?
df输入列包括“ AwardNumber”(在字典中为“ noticeNumber”和“ College”等)。数据框如下所示:
AwardAmount AwardNumber College Department Name Email
None 3R01GM110382-03S1 College of Arts and Sciences Chemistry Mary [email protected]
所需的输出将是这样的:
{
"AwardAmount": None,
"noticeNumber": "3R01GM110382-03S1",
"College": "College of Arts and Sciences",
"Department": "Chemistry",
"Name": "Mary",
"Email": "[email protected]"
}
查看to_dict方法。它具有orient
参数,该参数确定字典的类型。由于您没有提供多行示例,因此我只能猜测您想要得到什么结果。这是带有的示例orient="records"
:
df = pd.DataFrame([[None, "3R01GM110382-03S1", "College of Arts and Sciences", "Chemistry", "Mary", "[email protected]"]], columns=["AwardAmount", "AwardNumber", "College", "Department", "Name", "Email"])
(df.rename(columns={'AwardNumber': 'noticeNumber'})
.to_dict(orient="records"))
# [{'Department': 'Chemistry', 'Name': 'Mary', 'AwardAmount': None, 'noticeNumber': '3R01GM110382-03S1', 'College': 'College of Arts and Sciences', 'Email': '[email protected]'}]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句