使用带字典的方式批量修改数据类型
dtype_dict = {'A': 'int32',
'B': 'int8',
'C': 'int16',
'D': 'int8',
'E': 'int8',
'F': 'int8'
}
df.astype(dtype_dict)
结果出现问题:
KeyError Traceback (most recent call last)
in ()
6 'D': 'int8'
7 }
----> 8 df.astype(dtype_dict)
D:\AN\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors, **kwargs)
5671 for col_name in dtype.keys():
5672 if col_name not in self:
-> 5673 raise KeyError('Only a column name can be used for the '
5674 'key in a dtype mappings argument.')
5675 results = []
KeyError: 'Only a column name can be used for the key in a dtype mappings argument.'
经过分析,是因为字典里的key手误写错了导致的