简体   繁体   English

将嵌套的类似字典的 txt 文件读入 Pandas 数据帧

[英]Reading nested dictionary-like txt file into a Pandas dataframe

Sort of a new python guy here and haven't had much success with the following.这里是一个新的 python 人,并且在以下方面没有取得太大的成功。

I have a txt file with data formatted as follows:我有一个 txt 文件,其数据格式如下:

{
  "$type" : "TableInstance",
  "$version" : 1,
  "Instance" : "InstanceName",
  "ColumnAliases" : [ "", "", ],
  "ColumnNames" : [ "keyName", "dateName"],
  "ColumnData" : [ {
    "type" : "ColumnData1",
    "Strings" : [key1, key2],]
  }, {
    "type" : "ColumnData2",
    "Strings" : [date1, date2]}]
}

That I would like to read into a dataframe such that it is formatted as:我想读入一个数据帧,使其格式为:

[   keyName     dateName
    key1        date1
    key2        date1 ]

Is there a simple way to do this?有没有一种简单的方法可以做到这一点?

does this work for you?这对你有用吗?

dict = {
  "$type" : "TableInstance",
  "$version" : 1,
  "Instance" : "InstanceName",
  "ColumnAliases" : [ "", "", ],
  "ColumnNames" : [ "keyName", "dateName"],
  "ColumnData" : [ {
    "type" : "ColumnData1",
    "Strings" : ['key1', 'key2']
  }, {
    "type" : "ColumnData2",
    "Strings" : ['date1', 'date2']}]
}

df = pd.DataFrame({dict['ColumnNames'][0]:dict['ColumnData'][0]['Strings'], dict['ColumnNames'][1]:dict['ColumnData'][1]['Strings']})

It looks it that you stored the serialized python object in the file.看起来您将序列化的python对象存储在文件中。 Hence, you can deserialize the Python object by the help of pickle, then you can parse the object based on your requirements.因此,您可以通过 pickle 的帮助反序列化 Python 对象,然后您可以根据您的要求解析该对象。

import pickle
import pandas as pd

filePath = 'test.txt'
obj = pd.read_pickle(filePath)
#obj = pickle.load(open(filePath, "rb"))
df = pd.DataFrame({obj['ColumnNames'][0]:obj['ColumnData'][0]['Strings'], obj['ColumnNames'][1]:obj['ColumnData'][1]['Strings']})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM