简体   繁体   English

Python:数据框中的类字典对象到新数据框

[英]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.

相关问题 将嵌套的类似字典的 txt 文件读入 Pandas 数据帧 - Reading nested dictionary-like txt file into a Pandas dataframe pandas DataFrame 行的类字典 get() 方法? - dictionary-like get() method for rows of a pandas DataFrame? Java - 将字节数组转换为 python 类字典 object - Java - Converting byte array to a python dictionary-like object Python中类似字典的对象,允许设置任意属性 - Dictionary-like object in Python that allows setting arbitrary attributes 将包含重复变量名称和变量值的类字典对象列表转换为 Pandas dataframe - Converting a list of dictionary-like objects containing repeated variable names and variable values to a Pandas dataframe 如何获取类似字典的字符串中的值,并在 dataframe 的列中对 output 进行排序? - How to get values in a dictionary-like string and sort the output all within the column in a dataframe? C ++中的Python字典式结构? - Python dictionary-like structure in C++? 在Python中解析类似字典的URL参数 - Parsing dictionary-like URL parameters in Python 在 Python 中将具有类似字典表示的字符串转换为字典 - Transform string with dictionary-like representation into dictionary in Python 以类似字典的方式将新项添加到某个结构化数组中 - Add new items to some structured array in a dictionary-like way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM