栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python-pandas,dataframe报错dataframe type object ‘object‘ has no attribute ‘dtype‘

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-pandas,dataframe报错dataframe type object ‘object‘ has no attribute ‘dtype‘

问题

使用pandas创建新的dataframe时报错

    df=pd.Dataframe(columns=['code', 'name'])
    for row in results:
        i= i+1
        print(i)
        print("总数%s, 当前%s 编码%s" % (str(cursor.rowcount), str(i), row[0]))
        codevalue = row[0]
        namevalue = row[1]
		temp=[{'code':codevalue,'name':namevalue}]
		df=df.append(temp)

报错信息:

>>> df = pd.Dataframe(columns=result_columns)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/python3/lib/python3.7/site-packages/pandas/core/frame.py", line 392, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "/usr/local/python3/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 196, in init_dict
    nan_dtype)
  File "/usr/local/python3/lib/python3.7/site-packages/pandas/core/dtypes/cast.py", line 1175, in construct_1d_arraylike_from_scalar
    dtype = dtype.dtype
AttributeError: type object 'object' has no attribute 'dtype'
原因:

pandas的版本不对,需要更新或者升级版本。

解决办法:

# 如果想要使用最新版本的pandas使用命令安装:
pip install pandas --upgrade
# 或者你想使用特定版本的pandas使用以下命令安装:
pip install pandas=1.1.5

升级后再试就可以了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/665712.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号