pandas类型

用法一:修改某一列的数据类型
df: pd.dataframe = pd.dataframe([
['a', '1', '4.2'],
['b', '70', '0.03'],
['x', '5', '0']
], columns=['one', 'two', 'three'])
df['two'] = df['two'].astype('int64') # 修改'two'列为 int类型
| one | two | three |
|---|---|---|
| a | 1 | 4.2 |
| b | 70 | 0.03 |
| c | 5 | 0 |
用法二:修改多列的数据类型
df: pd.dataframe = pd.dataframe([
['a', '1', '4.2'],
['b', '70', '0.03'],
['x', '5', '0']
], columns=['one', 'two', 'three'])
df[['two', 'three']] = df[['two', 'three']].apply(pd.to_numeric) # 内置函数,to_numeric() 可以将一列转换为数值类型,自动判断是 int 还是 float
类似的内置函数还包括:pd.to_datetime(),转换成时间类型datetime,还有pd.to_timedelta()转换为时间戳类型
到此这篇关于pandas使用dtype/dtypes修改数据类型的文章就介绍到这了,更多相关pandas修改数据类型内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论