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

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

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

升级后再试就可以了。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5670983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存