无法使用python将字典打印为数据帧

无法使用python将字典打印为数据帧,python,pandas,dictionary,Python,Pandas,Dictionary,我有一个包含字典的文本文件,当尝试在类中使用pandas导入和打印dataframe时,它会将错误显示为“ValueError:dataframe构造函数未正确调用!”。但当尝试导入并打印在jupyter笔记本的cell中时。它能够打印数据帧。不知道哪里不对。数据帧的格式如下 {'id_1' : {'name': 'person1','height': u'150', 'weight': 56, 'age' : 20, 'DOB' : ''02 Aug 2005'}, 'id_2' :

我有一个包含字典的文本文件,当尝试在类中使用pandas导入和打印dataframe时,它会将错误显示为“ValueError:dataframe构造函数未正确调用!”。但当尝试导入并打印在jupyter笔记本的cell中时。它能够打印数据帧。不知道哪里不对。数据帧的格式如下

 {'id_1' : {'name': 'person1','height': u'150', 'weight': 56, 'age' : 20, 'DOB' : ''02 Aug 2005'}, 
  'id_2' : {'name': 'person2','height': u'145', 'weight': 50, 'age' : 22, 'DOB' : ''25 Sept 2005'}, 
  'id_3' : {'name': 'person3','height': u'165', 'weight': 65, 'age' : 25, 'DOB' : ''17 May 2004'}}

如果我认为有必要从文本文件进行解析,请首先通过
ast.literal\u eval
将字符串转换为dicts,然后调用:



如果我认为有必要从文本文件进行解析,请首先通过
ast.literal\u eval
将字符串转换为dicts,然后调用:



请出示您的代码请出示您的代码谢谢回复。但是,这段代码也给出如下错误:“ValueError:格式错误的节点或字符串:@Kedar17-如果使用
df=pd.DataFrame.from_dict(d,orient='index')
它正在工作吗?谢谢。这看起来是可行的,因为错误现在不同了。现在它将错误显示为:“AttributeError:'list'对象没有属性'values'”…感谢您的回复。但是,这段代码也给出如下错误:“ValueError:格式错误的节点或字符串:@Kedar17-如果使用
df=pd.DataFrame.from_dict(d,orient='index')
它正在工作吗?谢谢。这看起来是可行的,因为错误现在不同了。现在它将错误显示为:“AttributeError:'list'对象没有属性'values'”。。。
d = """{'id_1' : {'name': 'person1','height': u'150', 'weight': 56, 'age' : 20, 'DOB' : '02 Aug 2005'},
     'id_2' :{'name': 'person2','height': u'145', 'weight': 50, 'age' : 22, 'DOB' : '25 Sept 2005'}, 
     'id_3' : {'name': 'person3','height': u'165', 'weight': 65, 'age' : 25, 'DOB' : '17 May 2004'}}"""
import ast

df = pd.DataFrame.from_dict(ast.literal_eval(d), orient='index')
print (df)
         name height  weight  age           DOB
id_1  person1    150      56   20   02 Aug 2005
id_2  person2    145      50   22  25 Sept 2005
id_3  person3    165      65   25   17 May 2004