[英]Python: Dictionary-like object in dataframe to new dataframe
Hello I've done some webscraping and I have a dictionary-like object that is in one of the columns of my dataframe. 您好,我做了一些网页抓取工作,并且在数据框的某一列中有一个类似字典的对象。
data.offerAggregate.property_aggregate.property.floors
is the column name and the rest is a value inside the column. data.offerAggregate.property_aggregate.property.floors
是列名,其余是该列内的值。
data.offerAggregate.property_aggregate.property.floors
0 [{'units': [{'features': [{'Code': 'chairs', 'Exists': True}, {'Code': 'window', 'Exists': True}, {'Code': 'balcony', 'Exists': True}, {'Code': 'fridge', 'Exists': True}, {'Code': 'stove', 'Exists': True}, {'Code': 'oven', 'Exists': True}, {'Code': 'microwave', 'Exists': True}, {'Code': 'washing-machine', 'Exists': True}, {'Code': 'dryer', 'Exists': False}, {'Code': 'dishwasher', 'Exists': False}, {'Code': 'table', 'Exists': True}]}, {'features': [{'Code': 'toilet', 'Exists': True}, {'Code': 'sink', 'Exists': True}, {'Code': 'bathtub', 'Exists': False}, {'Code': 'shower', 'Exists': True}]}, {'features': [{'Code': 'wardrobe', 'Exists': True}, {'Code': 'chest-of-drawers', 'Exists': True}, {'Code': 'desk', 'Exists': True}, {'Code': 'chairs', 'Exists': True}, {'Code': 'sofa', 'Exists': False}, {'Code': 'sofa-bed', 'Exists': False}, {'Code': 'window', 'Exists': True}, {'Code': 'balcony', 'Exists': False}, {'Code': 'tv', 'Exists': False}]}, {'features': [{'Code': 'desk', 'Exists': False}, {'Code': 'chairs', 'Exists': True}, {'Code': 'sofa', 'Exists': True}, {'Code': 'sofa-bed', 'Exists': False}, {'Code': 'coffee-table', 'Exists': False}, {'Code': 'table', 'Exists': True}, {'Code': 'tv', 'Exists': False}]}, {'features': [{'Code': 'chest-of-drawers', 'Exists': True}, {'Code': 'desk', 'Exists': True}, {'Code': 'chairs', 'Exists': True}, {'Code': 'sofa', 'Exists': False}, {'Code': 'sofa-bed', 'Exists': False}, {'Code': 'window', 'Exists': True}, {'Code': 'balcony', 'Exists': False}]}]}]
What I would like to end up with is a new dataframe that has the Code values as the column names and the Exists values as the column value. 我要结束的是一个新的数据框,它具有Code值作为列名,Exists值作为列值。 So instead of
{'Code': 'chairs', 'Exists': True}
, I would have chairs
True
因此,我将拥有
chairs
True
,而不是{'Code': 'chairs', 'Exists': True}
I tried making it into a json object so I can access the keys and store them into a new dataframe, but I'm getting errors. 我尝试将其制作为json对象,以便可以访问密钥并将其存储到新的数据帧中,但是却遇到了错误。
df_floors = pd.DataFrame(df['data.offerAggregate.property_aggregate.property.floors'])
jsonObj = df_floors.to_json(orient='records')
print(jsonObj['Code'])
TypeError: string indices must be integers
I'm not sure if this is the right approach but any ideas or help is much appreciated! 我不确定这是否是正确的方法,但是任何想法或帮助都将不胜感激!
检查jsonObj的格式,以便查看其是否符合您的期望。
print(jsonObj)
pd.DataFrame(row[0]['units'][0]['features'])
produces: 生产:
Code Exists
0 chairs True
1 window True
2 balcony True
3 fridge True
4 stove True
5 oven True
6 microwave True
7 washing-machine True
8 dryer False
9 dishwasher False
10 table True
or 要么
print(pd.DataFrame([{x['Code']: x['Exists'] for x in row[0]['units'][0]['features']}]))
produces: 生产:
balcony chairs dishwasher dryer fridge microwave oven stove table
0 True True False False True True True True True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.