【问题标题】:TypeError: Unsupported type <class 'numpy.dtype'> in write()TypeError:不支持的类型 <class 'numpy.dtype'> in write()
【发布时间】:2018-08-20 09:56:06
【问题描述】:

所以我正在读取一个.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)

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

任何建议将不胜感激。

【问题讨论】:

    标签: python excel pandas dataframe python-applymap


    【解决方案1】:

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

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

    【讨论】:

    • 完美运行,非常感谢!!
    • @explorer_x - 欢迎您!如果我的回答有帮助,请不要忘记 accept 它 - 单击答案旁边的复选标记 (v) 将其从灰色切换为已填充。谢谢。
    • @explorer_x - 很高兴能帮上忙!
    猜你喜欢
    • 2016-12-27
    • 2021-06-02
    • 2016-02-23
    • 2020-03-06
    • 2021-08-01
    • 2022-11-11
    • 2020-12-16
    • 2020-02-29
    • 2011-03-08
    相关资源
    最近更新 更多