pandas不显示index_Pandas的DataFrame出现了问题,求教

在尝试创建一个pandas DataFrame时,使用了字典`pop`作为数据源并指定了一组整数作为索引。然而,代码在运行时遇到了一个错误,提示'list'对象没有属性'astype'。这通常发生在尝试将非序列对象转换为特定数据类型,如将列表转换为数值类型时。错误出现在试图将索引`index=[2001, 2002, 2003]`的值astype为'O'的过程中。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import pandas as pd

pop = {

'Nevada': {2001: 2.4, 2002: 2.9},

'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}

}

pd.DataFrame(pop, index=[2001, 2002, 2003])

然后爆出了如下错误

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last) in ()----> 1 pd.DataFrame(pop,index=[2001,2002,2003])C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy) 346 dtype=dtype, copy=copy) 347 elif isinstance(data, dict):--> 348 mgr = self._init_dict(data, index, columns, dtype=dtype) 349 elif isinstance(data, ma.MaskedArray): 350 import numpy.ma.mrecords as mrecordsC:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _init_dict(self, data, index, columns, dtype) 457 arrays = [data[k] for k in keys] 458 --> 459 return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) 460 461 def _init_ndarray(self, values, index, columns, dtype=None, copy=False):C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype) 7357 7358 # don't force copy because getting jammed in an ndarray anyway-> 7359 arrays = _homogenize(arrays, index, dtype) 7360 7361 # from BlockManager perspectiveC:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _homogenize(data, index, dtype) 7659 if isinstance(v, dict): 7660 if oindex is None:-> 7661 oindex = index.astype('O') 7662 7663 if isinstance(index, (DatetimeIndex, TimedeltaIndex)):AttributeError: 'list' object has no attribute 'astype'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值