首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据帧表示、列表和文本条目的字典

数据帧表示、列表和文本条目的字典
EN

Stack Overflow用户
提问于 2021-01-25 23:14:53
回答 3查看 41关注 0票数 0

我想要将字典条目更改为数据帧。示例:

代码语言:javascript
复制
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']
    }

我想要的输出是这样的:

代码语言:javascript
复制
        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

其中我通过以下方式修复

代码语言:javascript
复制
pd.DataFrame.from_dict(data1,orient='index').transpose()

然而,出现了两个问题:

第一:描述被截断。只有文本的这一部分出现:"Python是一种解释性的、高级的和流派的……“

第二:输出被分成两个条目,如上图所示。

如何解决这些问题?

EN

回答 3

Stack Overflow用户

发布于 2021-01-25 23:24:01

将类别转换为列表列表

代码语言:javascript
复制
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]
票数 1
EN

Stack Overflow用户

发布于 2021-01-25 23:23:16

您正在使用的DataFrame构造函数使用一个键,其中键是DataFrame中列的名称,并且对于每个键,如果您希望列类别中第一行的值是一个列表,那么对于每个键,您在列表中的列中都有您想要的值。则与'Categories'关联的列表的第一个元素必须是列表。作为一个简单的例子,考虑一下:

代码语言:javascript
复制
data1 = {'Site': ['Wikipedia'],
         'Categories': [ ['Python', 'Programming'] ]} # list[list[str]]
df = pd.DataFrame(data1)
票数 0
EN

Stack Overflow用户

发布于 2021-01-25 23:25:36

  1. 因为你只有一个项目,你需要把它传递给任何可迭代的对象(它是可用的变体之一)。例如,

代码语言:javascript
复制
data = [
    {
        "Description": "Python is an interpreted, high-level and general-purpose programming etc.",
        "Site": "Wikipedia",
        "Categories": ["Python", "Programming"]
    }
]
frame = pd.DataFrame(data)

  1. 描述不会被截断。这个展示只是一个预览。如果您通过frame["Description"]选择描述字段,您将看到完整的内容。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65887467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档