Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将Dataframe列转换为浮点_Python_Pandas_Numpy_Pytorch - Fatal编程技术网

Python 无法将Dataframe列转换为浮点

Python 无法将Dataframe列转换为浮点,python,pandas,numpy,pytorch,Python,Pandas,Numpy,Pytorch,我正在使用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.43

我正在使用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']
然后我需要将列的值转换为float,但是我得到了一个错误

features = np.stack([df[col].values for col in features], 1)
features = torch.tensor(features, dtype=torch.float)
features[:5]
熊猫给我看的错误是:

KeyError:“[Index([51.45440668101.4323397,64.91708906,'234.2488932',\n 2.470623484,96.72735193,1.87437372],\n dtype='object')中没有一个在[列]中。”


为什么不直接使用
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))

现在我得到了这个错误值错误:无法将字符串转换为float:“[-11059]没有用于计算的好数据”现在这个错误属性错误:“DataFrame”对象没有属性“str”,现在这个错误属性错误:(“只能使用带字符串值的.str访问器!”,“在索引01处发生”)您是否尝试在astype调用中添加errors='concurve'?这将为熊猫无法转换的任何值返回nan。可能有助于缩小范围。