简体   繁体   English

如何从pandas数据框创建字典?

[英]How to create a dict of dicts from pandas dataframe?

I have a dataframe df 我有一个数据框df

id      price      date         zipcode
u734    8923944    2017-01-05   AERIU87
uh72    9084582    2017-07-28   BJDHEU3
u029    299433     2017-09-31   038ZJKE

I want to create a dictionary with the following structure 我想用以下结构创建字典

{'id': xxx, 'data': {'price': xxx, 'date': xxx, 'zipcode': xxx}}

What I have done so far 到目前为止我做了什么

ids = df['id']
prices = df['price']
dates = df['date']
zips = df['zipcode']
d = {'id':idx, 'data':{'price':p, 'date':d, 'zipcode':z} for idx,p,d,z in zip(ids,prices,dates,zips)}
>>> SyntaxError: invalid syntax

but I get the error above. 但我得到上面的错误。

What would be the correct way to do this, using either 使用这两种方法的正确方法是什么

  • list comprehension 清单理解

OR 要么

  • pandas .to_dict() 熊猫.to_dict()

bonus points: what is the complexity of the algorithm, and is there a more efficient way to do this? 优点:算法的复杂度是多少?有没有更有效的方法?

I'd suggest the list comprehension. 我建议名单理解。

v = df.pop('id')
data = [
   {'id' : i, 'data' : j} 
   for i, j in zip(v, df.to_dict(orient='records'))
]

Or a compact version, 或精简版

data = [dict(id=i, data=j) for i, j in zip(df.pop('id'), df.to_dict(orient='r'))]

Note that, if you're popping id inside the expression, it has to be the first argument to zip . 请注意,如果要在表达式中弹出id ,则它必须zip的第一个参数。

print(data)
[{'data': {'date': '2017-09-31',
   'price': 299433,
   'zipcode': '038ZJKE'},
  'id': 'u029'},
 {'data': {'date': '2017-01-05',
   'price': 8923944,
   'zipcode': 'AERIU87'},
  'id': 'u734'},
 {'data': {'date': '2017-07-28',
   'price': 9084582,
   'zipcode': 'BJDHEU3'},
  'id': 'uh72'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM