Skip to main content
 首页 » 编程设计

python之类型错误 : Unsupported type class 'numpy.dtype' > in write()

2024年09月07日23开发

所以我正在读取一个 .xlsx 文件,我需要检查 xlsx 文件中有多少变量属于 pandas 中的每个数据类型,最后将其导出到 excel。

所以这里是代码:

sheet = pd.read_excel(r"D:\Users\850034535\AnacondaProjects\Jupyter Project\Sample.xlsx",sheetname=2) 
alldtypes = sheet.dtypes.value_counts() 
alldtypes_df = list_of_all_variables.to_frame() 
alldtypes_df.to_excel('123.xlsx') 

这段代码报错:“TypeError: float() argument must be a string or a number, not 'numpy.dtype'”

然后我将数据帧转换为 str 类型,所以现在添加了一行代码:

 alldtypes_df = alldtypes_df.applymap(str) 

但它仍然向我显示相同的错误和标题中给出的错误。

如有任何建议,我们将不胜感激。

请您参考如下方法:

似乎需要将索引转换为string:

alldtypes_df.index = alldtypes_df.index.astype(str)