[英]Python - Function for parsing key-value pairs into DataFrame columns
[英]python - How to transform key-value pairs in Pandas dataframe
我收到了这个数据集,其中包含 a.csv 格式的键值对中的房地产数据。
如果我删除第一行,我可以用 Pandas 加载它并得到一个 dataframe,如下所示:
编号 1 | [{'键'": '"地板'" | '"value'": '"2. Floor'"} | {'“关键'”:'“可用_日期'” | "价值'": '"nach Vereinbarung'"} |
编号 2 | [{'键'": '"地板'" | '"value'": '"1. Floor'"} | {'“钥匙'”:'“生活空间'” | “价值”:81.0} |
编号 3 | [{'key'": '"生活空间'" | '“价值”:240.0} | {'“key'”:'“construction_year'” | '“价值”:2012} |
编号 4 | [{'key'": '"生活空间'" | '“价值”:280.0} | {'“key'”:'“construction_year'” | '“价值”:1851} |
但是,我不知道如何使用 Python 中的键值对,所以我想将此数据转换为 Pandas dataframe,其中“键”作为标题,每行中它们各自的值,如下所示:
ID | 地面 | 可用_日期 | 居住空间 | 施工年 |
---|---|---|---|---|
编号 1 | 2.地板 | 交际会 | ||
编号 2 | 1.地板 | 81 | ||
编号 3 | 240.0 | 2012 | ||
编号 4 | 280.0 | 1851年 |
我找到了很多关于如何将 Pandas dataframe 转换为键值对的说明,但反之则不然......
先感谢您。
更新
我的数据内容如下所示:
print(df.head(10))
[{'key'": '"floor'" '"value'": '"3. Stock'"} {'"key'": '"living_space'" '"value'": 50.0} {'"key'": '"available_date'" ... Unnamed: 49 Unnamed: 50 Unnamed: 51 Unnamed: 52 Unnamed: 53
0 [{'key'": '"floor'" '"value'": '"2. Stock'"} {'"key'": '"living_space'" '"value'": 113.0} {'"key'": '"construction_year'" ... NaN NaN NaN NaN NaN
1 [{'key'": '"floor'" '"value'": '"1. Stock'"} {'"key'": '"living_space'" '"value'": 52.0} {'"key'": '"construction_year'" ... NaN NaN NaN NaN NaN
.. ... ... ... ... ... ... ... ... ... ... ...
8 [{'key'": '"living_space'" '"value'": 240.0} {'"key'": '"construction_year'" '"value'": 2012} {'"key'": '"available_date'" ... NaN NaN NaN NaN NaN
9 [{'key'": '"living_space'" '"value'": 280.0} {'"key'": '"construction_year'" '"value'": 1851} {'"key'": '"available_date'" ... NaN NaN NaN NaN NaN
[10 rows x 54 columns]
更新
.csv 的内容如下所示(对于第 2 次观察):
1,[{'key'"": '""floor'"""," '""value'"": '""3. Stock'""}","{'""key'"" : '""living_space'"""," '""value'"": 50.0}"," {'""key'"": '""available_date'"""," '""value'"" : '""01.04.2022'""}"," {'""键'"": '""有用区域'""", """值'"": 60.0}","{'"" key'"": '""pets_allowed'"""," '""value'"": true}"," {'""key'"": '""child_friendly'"""," '"" value'"": true}"," {'""key'"": '""balcony'"""," '""value'"": true}"," {'""key'"" : '""parking_outdoor'"""," '""value'"": true}"," {'""key'"": '""lift'"""," '""value'"" : true}"," {'""key'"": '""cable_tv'"""," '""value'"": true}]""","[{'date'"": ' ""2022-02-25'""","'""price_amount'"": 1550}]"""
2,[{'key'"": '""floor'"""," '""value'"": '""2. Stock'""}","{'""key'"" : '""living_space'"""," '""value'"": 113.0}"," {'""key'"": '""construction_year'"""," '""value'"" : 2022}"," {'""key'"": '""available_date'"""," '""value'"": '""01.04.2022'""}","{'"" key'"": '""wheelchair_accessible'"""," '""value'"": true}"," {'""key'"": '""child_friendly'"""," '"" value'"": true}"," {'""key'"": '""balcony'"""," '""value'"": true}"," {'""key'"" : '""parking_indoor'"""," '""value'"": true}"," {'""key'"": '""lift'"""," '""value'"" : true}]""","[{'日期'"": '""2022-02-27'"""," '""price_amount'"": 2990}]"""
这些数据似乎是从房地产在线市场中删除的。 我认为也与 state 有关,即每次观察都有不同数量的特征。
可能的解决方案如下:
文件“data.csv”内容
1,"[{'key'"": '""floor'"""," '""value'"": '""3. Stock'""}"," {'""key'"": '""living_space'"""," '""value'"": 50.0}"," {'""key'"": '""available_date'"""," '""value'"": '""01.04.2022'""}"," {'""key'"": '""useful_area'"""," '""value'"": 60.0}"," {'""key'"": '""pets_allowed'"""," '""value'"": true}"," {'""key'"": '""child_friendly'"""," '""value'"": true}"," {'""key'"": '""balcony'"""," '""value'"": true}"," {'""key'"": '""parking_outdoor'"""," '""value'"": true}"," {'""key'"": '""lift'"""," '""value'"": true}"," {'""key'"": '""cable_tv'"""," '""value'"": true}]""","[{'date'"": '""2022-02-25'"""," '""price_amount'"": 1550}]"""
2,"[{'key'"": '""floor'"""," '""value'"": '""2. Stock'""}"," {'""key'"": '""living_space'"""," '""value'"": 113.0}"," {'""key'"": '""construction_year'"""," '""value'"": 2022}"," {'""key'"": '""available_date'"""," '""value'"": '""01.04.2022'""}"," {'""key'"": '""wheelchair_accessible'"""," '""value'"": true}"," {'""key'"": '""child_friendly'"""," '""value'"": true}"," {'""key'"": '""balcony'"""," '""value'"": true}"," {'""key'"": '""parking_indoor'"""," '""value'"": true}"," {'""key'"": '""lift'"""," '""value'"": true}]""","[{'date'"": '""2022-02-27'"""," '""price_amount'"": 2990}]"""
import pandas as pd
import json
# read data from csv file
with open("data.csv", "r", encoding="utf-8") as file:
data = file.read().replace('"', '').replace("'", '"').replace("[", '').replace("]", '').splitlines()
# convert string to list
data_dict = [json.loads("[" + d + "]") for d in data]
data_all = []
for list_item in data_dict:
data_prepared = {}
for idx, item in enumerate(list_item):
if idx == 0:
data_prepared["id"] = item
else:
if 'key' in item:
data_prepared[item['key']] = item['value']
else:
data_prepared.update(item)
data_all.append(data_prepared)
# create dataframe
df = pd.DataFrame(data_all)
df = df.fillna("-")
df = df.replace(True, 'Yes')
df = df.replace(False, 'No')
df
退货
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.