Skip to main content
 首页 » 编程设计

python之将字典转换为 Python 数据框

2024年11月24日8rubylouvre

<分区>

Dict = {'type': 'book', 'text_eng': 'National accounts', 'text': 'Volkswirtschaftliche Gesamtrechnungen', 'anchor': 'GESAMTVOLK'} 
pd.DataFrame(Dict) 
pd.DataFrame.from_dict(Dict) 

上面的脚本抛出错误:“ValueError:如果使用所有标量值,则必须传递一个索引”

我需要字典键作为 DataFrame 列和行中的字典值。

下面的脚本工作正常,因为字典值添加为列表

Dict = {'type': ['book'], 'text_eng': ['National accounts'], 'text': ['Volkswirtschaftliche Gesamtrechnungen'], 'anchor': ['GESAMTVOLK']} 

请让我知道如何实现这一点?