为何读取Excel后最后一列数据类型为str而非float?
问题:Excel文本存储数字读取为DataFrame后列类型不一致
问题背景
- Excel文件中B列至K列的所有数据均为以文本形式存储的数字。
- 将测试用Excel文件保存至路径:
/tmp/tsm.xlsx
读取后的异常现象
使用pandas读取该文件的ratios_annual工作表后,发现最后一列K的数据类型为字符串(str),但B至J列均为数值类型(numpy.float64)。测试代码及输出如下:
import pandas as pd sexcel = '/tmp/tsm.xlsx' df = pd.read_excel(sexcel,sheet_name='ratios_annual') row_num = len(df) for id in range(row_num): print('the data type in last column--K is',type(df.iloc[id,-1])) print('the data type in column--J is',type(df.iloc[id,-2]))
输出结果:
the data type in last column--K is <class 'str'> the data type in column--J is <class 'numpy.float64'> the data type in last column--K is <class 'str'> the data type in column--J is <class 'numpy.float64'>
疑问
Excel中B至K列均为以文本形式存储的数字,为何读取为DataFrame后各列数据类型出现差异?
内容的提问来源于stack exchange,提问作者showkey
相关产品推荐
相关产品推荐

