【发布时间】:2020-05-04 16:55:49
【问题描述】:
所以我尝试了如何在数据框中转换字典的资源,但问题是这是一个奇怪的字典。
不像key: {} , key: {} and etc..
数据有很多项目。但目标是仅提取 dict {} 中的内容,如果可能的话,日期也是一个加号。
数据:
id,client,source,status,request,response,queued,created_at,updated_at
54252,sdf,https://asdasdadadad,,"{
"year": "2010",
"casa": "aca",
"status": "p",
"Group": "57981",
}",,1,"2020-05-02 11:06:17","2020-05-02 11:06:17"
54252,msc-lp,https://discover,,"{
"year": "27",
"casa": "Na",
"status": "p",
"Group": "57981",
}"
我的尝试:
#attempt 1
with open('data.csv') as fd:
pairs = (line.split(None) for line in fd)
res = {int(pair[0]):pair[1] for pair in pairs if len(pair) == 2 and pair[0].isdigit()}
#attempt 2
import json
# reading the JSON data using json.load()
file = 'data.json'
with open(file) as train_file:
dict_train = json.load(train_file)
# converting json dataset from dictionary to dataframe
train = pd.DataFrame.from_dict(dict_train, orient='index')
train.reset_index(level=0, inplace=True)
#attempt 3
df = pd.read_csv("data.csv")
df = df.melt(id_vars=["index", "Date"], var_name="variables",value_name="values")
由于数据形状怪异,没有任何效果
预期输出:
字典中的所有项目,每个键将是 df 的一列
Date year casa status Group
2020-05-02 11:06:17 2010 aca p 57981
2020-05-02 11:06:17 27 Na p 57981
【问题讨论】:
-
需要将最后一列解析为json
-
如何只访问最后一列,就像我的循环将全部运行 @Aqua 4
-
您的 csv 格式错误,它应该只有 2 行,但由于 json 对象,您也有“\n”。在创建时,您可以删除该 json 的“\n”并将其放在一行中吗?
-
对不起,我不明白,什么是“\n”?
标签: python json pandas dictionary