在 python pandas 中从相同 csv(我自己的 json 格式)中的值创建一个 Json 列
Posted
技术标签:
【中文标题】在 python pandas 中从相同 csv(我自己的 json 格式)中的值创建一个 Json 列【英文标题】:Create a Json column from values in the same csv (My own json format) in python pandas 【发布时间】:2020-07-26 03:41:57 【问题描述】:我有一个数据框-
+----------+----------+--------+
| FNAME| LNAME| AGE| JSON FOR EACH CUSTOMER
+----------+----------+--------+
| EARL| JONES| 35| ?
| MARK| WOOD| 20| ?
+----------+----------+--------+
而且我有一个 json 格式 -
"applicantSegment":
"applicantName":
"name1": "" ,
"name3": ""
,
"ids": [
"type": "",
"value": ""
],
"phones": [
"type": "",
"value":""
],
"emails": [
"value": ""
],
"dob":
"dobDate": "" ,
"age" : ""
我需要在数据框中创建一个新的 json 列,以便新列的每一行都有 json 值。例如,第一行的新列值将是 -
"applicantSegment":
"applicantName":
"name1": "EARL" ,
"name3": "JONES"
,
"ids": [
"type": "",
"value": ""
],
"phones": [
"type": "",
"value":""
],
"emails": [
"value": ""
],
"dob":
"dobDate": "" ,
"age": 35
有人可以帮忙吗? 具有新 json 行的必需数据框
+----------+----------+--------+--------------------------------------------------------------
| FNAME| LNAME| AGE| JSON FOR EACH CUSTOMER
+----------+----------+--------+--------------------------------------------------------------
| EARL| JONES| 35| "applicantSegment": "applicantName"name1":
| | | "EARL","name3":"JONES" ,"ids": ["type": "","value": ""],
| | | "phones""type":"","value":"" ],"emails": ["value": "" ],
| | | "dob": "dobDate": "" ,"age":35
|----------|----------|--------| ---------------------------------------------------------------
| | | Same as above but values from this row
| MARK| WOOD| 20|
【问题讨论】:
【参考方案1】:这是一个 hack,但我之前回答错了你,感觉很糟糕,所以我想我会给你一些至少像你描述的那样有效的东西,直到其他人给出更好的答案。
import pandas as pd
import json
df = pd.DataFrame('FNAME': ['EARL','MARK'],
'LNAME': ['JONES','WOOD'],
'AGE': ["35","20"])
df['json_representation'] = """ "applicantSegment": "applicantName": "name1": """ + df['FNAME'] + """, "name3": """ + df['LNAME'][i] + """ , "ids": [ "type": "", "value": "" ], "phones": [ "type": "", "value":"" ], "emails": [ "value": "" ], "dob": "dobDate": "" , "age" : """ + df['AGE'][i] + """ )"""
我认为真正的解决方案是您应该使用 python 字典来构建它,但我没有看到您有任何可以用作键的独特项。
d = "applicantSegment": "applicantName": "name1": "" , "name3": "" , "ids": [ "type": "", "value": "" ], "phones": [ "type": "", "value":"" ], "emails": [ "value": "" ], "dob": "dobDate": "" , "age" : ""
s = ""
for i in df.index:
d.update( "applicantSegment": "applicantName": "name1": df['FNAME'][i] , "name3": df['LNAME'][i] , "ids": [ "type": "", "value": "" ], "phones": [ "type": "", "value":"" ], "emails": [ "value": "" ], "dob": "dobDate": "" , "age" : df['AGE'][i] )
s = s + ', ' + json.dumps(d)
s
【讨论】:
嗨,乔希,有没有办法在没有 for 循环的情况下解决这个问题,我有大约 50 万条记录?以上是关于在 python pandas 中从相同 csv(我自己的 json 格式)中的值创建一个 Json 列的主要内容,如果未能解决你的问题,请参考以下文章
我可以在 python3 中从 excel 文件(不是 CSV)创建字典吗?
如何在streamlit中从用户读取csv文件并转换为pandas数据框
如何在 python 的 S3 中从 Pandas 数据帧写入镶木地板文件