如何在Panda';s来自_dict(Python)

如何在Panda';s来自_dict(Python),python,dictionary,pandas,dataframe,Python,Dictionary,Pandas,Dataframe,我有一本字典: d = {1: 'hello'} 我想将其转换为Panda的数据帧(因此是一个以字符串作为行值的数据帧) 我正在看: pd.DataFrame.from_dict(d,orient='columns',dtype=?) 如果省略“dtype”参数,则得到: ValueError: If using all scalar values, you must pass an index 使用“orient='columns'”意味着将键用作列。但是如何使用数据类型(我假设错误是由于

我有一本字典:

d = {1: 'hello'}
我想将其转换为Panda的数据帧(因此是一个以字符串作为行值的数据帧)

我正在看: pd.DataFrame.from_dict(d,orient='columns',dtype=?)

如果省略“dtype”参数,则得到:

ValueError: If using all scalar values, you must pass an index
使用“orient='columns'”意味着将键用作列。但是如何使用数据类型(我假设错误是由于num/string问题造成的)

答案摘要: 将“d”构造为:

然后使用:

pd.DataFrame.from_dict(d)

谢谢

如果未通过索引,则需要将“hello”括起来

>>> pd.DataFrame.from_dict({1: ['hello']}, orient='columns', dtype=str)
       1
0  hello
请注意,“columns”是orient的默认值,不是必需的。此外,如果没有明确给出数据类型,则会推断出数据类型。

Ah!所以使用“{1:'hello'}”不起作用,它需要一个列表?嗯(我猜一个字符串已经是一个列表)是的,例如“{1:['hello','world']}”
>>> pd.DataFrame.from_dict({1: ['hello']}, orient='columns', dtype=str)
       1
0  hello