pandas将表中的字符串转成数值型

在用pd.read_csv读数据时,将要转换数据类型的列名和类型名构成字典,传给dtype

import numpy as np
import pandas as pd

path = 'house_data.csv'
col = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS',
       'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']
dict_dtype = { }
for i in col:
    dict_dtype[i] = np.float64
# print(dict_dtype)

data = pd.read_csv(path, header=None, names=col, sep=',', dtype=dict_dtype)
    原文作者:huluhuluwa123
    原文地址: https://blog.csdn.net/Blankit1/article/details/102864579
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞