繁体   English   中英

无法将 Pandas Dataframe 列转换为浮动

[英]Cannot convert Pandas Dataframe columns to float

我正在使用 Pandas 读取包含必须转换为浮点数的几列的 CSV 文件:

    df = pd.read_csv(r'dataset.csv',  low_memory=False,  sep = ',')
    df.head(2)

    Coal Flow 01    Air Flow 01 Outlet Temp 01  Inlet Temp 01   Bowl DP 01  Current 01  Vibration 01
    0   51.454407   101.432340  64.917089   234.2488932 2.470623    96.727352   1.874374
    1   51.625368   100.953089  64.726890   233.2340394 2.495698    96.309512   1.996391

接下来,我在名为features的变量中指定需要转换为浮点数的列:

    features = ['Coal Flow 01', 'Air Flow 01', 'Outlet Temp 01', 'Inlet Temp 01',
           'Bowl DP 01', 'Current 01', 'Vibration 01']

然后我需要将列的值转换为浮点数,但出现错误。

features = np.stack([df[col].values for col in features], 1)
features = torch.tensor(features, dtype=torch.float)
features[:5]

熊猫向我展示的错误是:

KeyError: "[Index([ 51.45440668, 101.4323397, 64.91708906, '234.2488932',\\n 2.470623484, 96.72735193, 737] ] 列中的任何一个 [索引([ 51.45440668, 101.4323397, 64.91708906, '2.470623484, 96.72735193, 737] ] 738 738 738 列

为什么不直接使用astype

df = pd.read_csv(r'dataset.csv',  low_memory=False,  sep = ',')
df[features] = df[features].apply(lambda x: x.apply(lambda x: x[0]).astype(float))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM