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