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 "<stdin>", line 1, in <module>
  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

升级后再试就可以了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值