You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将字典转换为Pandas DataFrame时生成行数超出预期是什么原因

问题原因

你的问题出在字典中tags字段的值是长度为5的列表:

  • Pandas 将字典转换为 DataFrame 时,默认会将可迭代的序列类型按元素拆分为多行,其他标量类型(字符串、数字等)会自动广播填充到对应行数,因此最终生成了5行数据。
解决方案

方案1:修改tags字段格式

将tags字段的列表再包裹一层列表,让Pandas将整个列表识别为单个单元格的值:

dff = pd.DataFrame(
{
'CEO': 'ucMMe Mhll', 
'address': 'vs5dlt3 B Se1kC eve0nre', 
'address2': '-', 
'city': 'a CSatanral', 
'companyName': 'Agilent Technologies Inc.', 
'country': 'nUatei tdetSs', 
'description': "tns oo el' yty", 
'employees': 17124, 
'exc': 'gdgdgd', 
'industry': 'sgeiTeotiroaLbtans r', 
'issueType': 'abc', 
'phone': '14087832319', 
'primarySicCode': 4008, 
'sector': ',atnSii Scilcofe,nnse TecisaPliinafs cedorhv cre', 
'securityName': 'elooIne.nen htc iisTcgAgl', 
'state': 'ailairofnC', 
'symbol': 'A', 
'tags': [['nllh he', 'gth', 'acsl', 'isiad', 'nr aitT']], 
'website': 'win.gcm.', 
'zip': '0752501-19'} )

方案2:将字典外层包裹列表

直接将整个字典放在列表中传入构造函数,指定单个字典对应1行数据:

dff = pd.DataFrame([
{
'CEO': 'ucMMe Mhll', 
'address': 'vs5dlt3 B Se1kC eve0nre', 
'address2': '-', 
'city': 'a CSatanral', 
'companyName': 'Agilent Technologies Inc.', 
'country': 'nUatei tdetSs', 
'description': "tns oo el' yty", 
'employees': 17124, 
'exc': 'gdgdgd', 
'industry': 'sgeiTeotiroaLbtans r', 
'issueType': 'abc', 
'phone': '14087832319', 
'primarySicCode': 4008, 
'sector': ',atnSii Scilcofe,nnse TecisaPliinafs cedorhv cre', 
'securityName': 'elooIne.nen htc iisTcgAgl', 
'state': 'ailairofnC', 
'symbol': 'A', 
'tags': ['nllh he', 'gth', 'acsl', 'isiad', 'nr aitT'], 
'website': 'win.gcm.', 
'zip': '0752501-19'} ])

两种方案都可以生成你预期的1行数据。

内容的提问来源于stack exchange,提问作者edn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 06:36:03