Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在处理多个 CSV 表的数据集。其中一个由两列组成的表具有带有 512 个字符串形式的元素的浮点数组。该表非常大,所以我想知道是否有任何有效的方法可以将它们转换为列形式的实际数组,而不是简单地遍历记录。
原Table的形状:(几百万条记录,2条)
所需的输出形状:(几百万条记录,513)或者可能(几百万条记录,2),但现在第二列包含 NumPy 数组而不是字符串。
所以,基本上你想要的是转换strings存储在具有一些浮点值的 pandas 数据帧中,比如'1.05'转换为一个 numpy.array 数组。
strings
'1.05'
那么您可以将这些字符串转换为浮点数,如下所示:
df['col1'] = df['col1'].astype(float)
然后您可以将整个数据帧转换为 numpy 数组,如下所示:
array = df.to_numpy()