Python 创建emptydataframe时出错
我试着制作一个虚拟数据框Python 创建emptydataframe时出错,python,pandas,dataframe,Python,Pandas,Dataframe,我试着制作一个虚拟数据框 列名=[“a”、“b”、“c”] df=pd.DataFrame(列=列名称) 我得到以下错误,这是以前没有发生过的,我错过了什么。这只发生在创建空数据帧时,这是最近引入的错误 File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 411
列名=[“a”、“b”、“c”]
df=pd.DataFrame(列=列名称)
我得到以下错误,这是以前没有发生过的,我错过了什么。这只发生在创建空数据帧时,这是最近引入的错误
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 411, in __init__
mgr = init_dict(data, index, columns, dtype=dtype)
File "/usr/local/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 242, in init_dict
val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype)
File "/usr/local/lib/python3.7/site-packages/pandas/core/dtypes/cast.py", line 1221, in construct_1d_arraylike_from_scalar
dtype = dtype.dtype
AttributeError: type object 'object' has no attribute 'dtype'
文件“”,第1行,在
文件“/usr/local/lib/python3.7/site packages/pandas/core/frame.py”,第411行,在__
mgr=init_dict(数据、索引、列、数据类型=dtype)
文件“/usr/local/lib/python3.7/site packages/pandas/core/internals/construction.py”,第242行,在init_dict中
val=从标量(np.nan、len(索引)、nan\u数据类型)构造类似于数组的数组
文件“/usr/local/lib/python3.7/site packages/pandas/core/dtypes/cast.py”,第1221行,从标量开始构建
dtype=dtype.dtype
AttributeError:类型对象“object”没有属性“dtype”
熊猫==0.25.3 更新至最新的
pandas==1.2.1
更新:
这是由于numpy软件包1.20.0
,
因此我锁定了numpy包,numpy==1.19.5
,pandas==0.25.3
如果您想知道为什么不想不必要地更新程序包,请参考此文件,因为
numpy==1.20.0
,并使用numpy==1.19.5
解决此问题。
熊猫版本可能不相关,在我的情况下,
pandas==1.0.4
熊猫版本是我的问题所在
如果你从
pandas==0.25.3转换为pandas==1.2.3如果出于某种原因您没有/无法升级numpy/pandas,另一种解决方法是在创建数据帧时指定数据类型。例如:
column_names = ["a", "b", "c"]
df = pd.DataFrame(columns = column_names, dtype=object)
您能提供完整的代码吗?您的代码中还有什么其他内容?这就是我正在尝试的全部内容,在终端@epsi95中,您使用哪个版本的pandas?我无法重现pandas 1.1.4中的错误。当您尝试更新您的pandas时,它可能解决了您的问题。