我想用数据框架中包含的值填充大量字典。理想情况下,每行应该用来填充一个字典,该字典将被附加到一个字典列表中。
#example df
firstname | lastname | dob | modified | created
john smith 1990-01-01 2019-10-11 2015-06-25
becca meyers 1995-04-13 2020-01-08 2018-05-18
每个字典看起来应该是这样的:(请忽略这个事实,在这个例子中这是没有意义的,这就是必须要做的)
d = {
"firstname" : {
"value": df['firstname'],
"modified": df['modified'],
"created": df['created']
},{
"lastname": {
"value": df['lastname'],
"modified": df['modified'],
"created": df['created']
}
}
我试过通过df做一个for
循环,但它只是在每个字段中填充整个数据帧,这不是我所希望的。
l = []
for x in df:
d = {
"firstname" : {
"value": df['firstname'],
"modified": df['modified'],
"created": df['created']
},{
"lastname": {
"value": df['lastname'],
"modified": df['modified'],
"created": df['created']
}
}
l.append(d)
ninjaedit:我的预期输出应该是:
l = [{
"firstname": {
"value": "john",
"modified": 2019-10-11,
"created": 2015-06-25}
},{
"lastname": {
"value": "smith",
"modified": 2019-10-11,
"created": 2015-06-25}},
...
}]
我已经有几个月没有做任何python
/pandas
了,所以我有点生疏了。提前感谢。如果这让你感到困惑,我很抱歉,如果需要的话,我可以展开。
使用DataFrame.itertuples
:
l = []
for x in df.itertuples():
d = {
"firstname" : {
"value": x.firstname,
"modified": x.modified,
"created": x.created
},
"lastname": {
"value": x.lastname,
"modified": x.modified,
"created": x.created
}
}
l.append(d)
print (l)
[{'firstname': {'value': 'john', 'modified': '2019-10-11', 'created': '2015-06-25'},
'lastname': {'value': 'smith', 'modified': '2019-10-11', 'created': '2015-06-25'}},
{'firstname': {'value': 'becca', 'modified': '2020-01-08', 'created': '2018-05-18'},
'lastname': {'value': 'meyers', 'modified': '2020-01-08', 'created': '2018-05-18'}}]
Slowier选择:
l = []
for i, x in df.iterrows():
d = {
"firstname" : {
"value": x.firstname,
"modified": x.modified,
"created": x.created
},
"lastname": {
"value": x.lastname,
"modified": x.modified,
"created": x.created
}
}
l.append(d)
你应该在循环中使用div .iterrows()
这里有一篇关于如何迭代pandas dataframe的小文章