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

标签 python excel pandas dataframe python-applymap

所以我正在读取一个.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() 参数必须是字符串或数字,而不是 'numpy.dtype'”

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

 alldtypes_df = alldtypes_df.applymap(str)

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

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

最佳答案

似乎需要将索引转换为字符串:

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

关于python - 类型错误 : Unsupported type <class 'numpy.dtype' > in write(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49234227/

相关文章:

Python 2 多处理映射,无需耗尽生成器

python - Adobe After Effects COM 对象模型 ID?

python - pandas 保留具有多个分隔符的行

mysql - Microsoft Excel SQL 查询中的多个数据源

python - 将 Pandas 数据框中 2 个类别的值计入数据透视表

python - df1 中不在 df2 中的所有行

python - 用附加函数拟合多项式

vba - 使用 Excel 中的查找并打开/关闭程序

vba - 创建 Excel 宏以一次删除多行

sql - 相当于 postgres sql 中的 pandas groupby ('v1' ).apply(lambda x : ['v2' ]. nunique())