python - 转换为 DataFrame 时,如何防止字典内的列表拆分成行?

标签 python python-3.x pandas dataframe

我有以下内容:

a = {
  "url": "http://www.example.com",
  "list": ["abc", "def", "ghi"]
}
DataFrame(a)
*****Output*****
                      url list
0  http://www.example.com  abc
1  http://www.example.com  def
2  http://www.example.com  ghi

我想要实现的是这样的:

                      url             list
0  http://www.example.com  [abc, def, ghi]

如何获得预期的输出?我尝试将 a["list"] 转换为 DataFrameDataFrame(a, index=[0]) 以及来自谷歌搜索的其他想法,但没有任何效果。

最佳答案

您可以将列表放入列表中。

a = {
  "url": "http://www.example.com",
  "list": [["abc", "def", "ghi"]]
}

关于python - 转换为 DataFrame 时,如何防止字典内的列表拆分成行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56229473/

相关文章:

python - 如何在 Python 中使用正则表达式从字符串中提取数字,除非在括号内?

python - 从脚本启动 Jenkins 参数化构建

python - 将 Pandas DataFrame 保存到 Django 模型

Python:按 CSV 中的唯一列合并重复项

python - pydot 中的嵌套集群

python - 不调用 Python 3 的多处理函数

python - 为什么这个 while 循环会结束? node5 不是 NoneType。 (遍历链表)

python - 解析 JSON 对象时,保留字作为数据类中的属性名称

python - 在 Pandas 中分割字符串忽略大小写

python - 属性错误 : 'CrossEntropyLoss' object has no attribute 'backward'