パンダはデータ型を変更します

パンダタイプ

ここに画像の説明を挿入します

使用法1:列のデータ型を変更する

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类型
1
a 1 4.2
b 70 0.03
c 5 0

使用法2:複数の列のデータ型を変更する

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()日時型pd.to_timedelta()への変換、およびタイムスタンプ型への変換

おすすめ

転載: blog.csdn.net/weixin_35757704/article/details/114572918