我想要将字典条目更改为数据帧。示例:
data1 ={'Description': ['Python is an interpreted, high-level and general-purpose programming language. Python\'s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.[28]'],
'Site': ['Wikipedia'],
'Categories': ['Python', 'Programming']
}我想要的输出是这样的:
Description Site Categories
0 Python is an interpreted, high-level and Wikipedia [Python, Programming]
general-purpose programming language. Python's
design philosophy emphasizes code readability
with its notable use of significant whitespace.
Its language constructs and object-oriented
approach aim to help programmers write clear,
logical code for small and large-scale projects
.[28]如果我使用pd.DataFrame.from_dict(data1),我会得到这个错误
ValueError: arrays must all be same length
其中我通过以下方式修复
pd.DataFrame.from_dict(data1,orient='index').transpose()然而,出现了两个问题:

第一:描述被截断。只有文本的这一部分出现:"Python是一种解释性的、高级的和流派的……“
第二:输出被分成两个条目,如上图所示。
如何解决这些问题?
发布于 2021-01-25 23:24:01
将类别转换为列表列表
data1 ={'Description': ['Python is an interpreted, high-level and general-purpose programming language. Python\'s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.[28]'],
'Site': ['Wikipedia'],
'Categories': [['Python', 'Programming']]
}
In[19]: df
Out[19]:
Description ... Categories
0 Python is an interpreted, high-level and gener... ... [Python, Programming]
[1 rows x 3 columns]发布于 2021-01-25 23:23:16
您正在使用的DataFrame构造函数使用一个键,其中键是DataFrame中列的名称,并且对于每个键,如果您希望列类别中第一行的值是一个列表,那么对于每个键,您在列表中的列中都有您想要的值。则与'Categories'关联的列表的第一个元素必须是列表。作为一个简单的例子,考虑一下:
data1 = {'Site': ['Wikipedia'],
'Categories': [ ['Python', 'Programming'] ]} # list[list[str]]
df = pd.DataFrame(data1)发布于 2021-01-25 23:25:36
data = [
{
"Description": "Python is an interpreted, high-level and general-purpose programming etc.",
"Site": "Wikipedia",
"Categories": ["Python", "Programming"]
}
]
frame = pd.DataFrame(data)frame["Description"]选择描述字段,您将看到完整的内容。https://stackoverflow.com/questions/65887467
复制相似问题