简体   繁体   English

将2D numpy数组的每一行导出为csv列中的单个元素

[英]Exporting each row of a 2D numpy array as a single element in csv column

I have a 2D numpy array which has a shape of (15015,262). 我有一个二维(numpy)数组,其形状为(15015,262)。 I need to export it to a csv file so that each row of the numpy 2Darray is a single element list of the csv file (ie, the csv file should have a shape of (15015,1). 我需要将其导出到一个csv文件,以便numpy 2Darray的每一行都是该csv文件的单个元素列表 (即,该csv文件的形状应为(15015,1)。

I tried using the csv modules of python and the numpy modules but nothing seems to work properly. 我尝试使用python和numpy模块的csv模块,但似乎没有任何正常工作。 For example, I tried using 例如,我尝试使用

with open('filename.csv', 'wb') as f: writer = csv.writer(f) for val in modified_seq_data: writer.writerow([val])

In the above case (even though the csv file does get saved in a single column), when I call it back via np.genfromtxt (for a different code) I get the following output: 在上述情况下(即使csv文件确实保存在单列中),当我通过np.genfromtxt(针对不同的代码)进行调用时,也会得到以下输出:

[nan nan nan nan nan ..... nan nan nan]

Somehow it is not able to export the csv into a numpy 1D array where each element is a list. 不知何故,它无法将csv导出到每个元素都是列表的numpy 1D数组中。 Any help will be much appreciated! 任何帮助都感激不尽!

Assuming that modified_seq_data is the numpy array: 假设modified_seq_data是numpy的数组:

with open('filename.csv', 'wb') as f:
    writer = csv.writer(f)
    for i in range(modified_seq_data.shape[0]):
        writer.writerow([modified_seq_data[i, :]])

Does your single output list object have commas in it? 您的单个输出列表对象中是否包含逗号? If so, your reader could just be reading into those commas through the list. 如果是这样,您的读者可能只是通过列表阅读了这些逗号。 As far as the structure goes, which I think is the underlying issue here, you need 1 atomic value in each row to get the effect of the csv. 就结构而言,我认为这是根本问题,每行需要1个原子值才能获得csv的效果。 So I would consider casting your list object into a big string separated by spaces, writing that out, then reading it in, splitting it, and casting the type back to what it is needed to be. 因此,我考虑将您的列表对象转换为由空格分隔的大字符串,将其写出,然后读入,拆分,然后将类型转换回所需的值。

Hope this helps. 希望这可以帮助。 BC 公元前

暂无
暂无

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

相关问题 2D 到 1D numpy 数组,每行的列索引 - 2D to 1D numpy array with indices of column for each row Numpy:如何将单个数组堆叠成更大数组的每一行并将其变成二维数组? - Numpy: How to stack a single array into each row of a bigger array and turn it into a 2D array? Numpy 检查二维 numpy 数组每一行的所有元素是否相同 - Numpy check that all the element of each row of a 2D numpy array is the same numpy 2d数组,将每行的每个索引剪切到该索引和特定列的最小值 - Numpy 2d array, clipping each index of each row to the minimum of that index and a specific column 计算2D NumPy数组中每行和每列内的非零元素 - Counting non-zero elements within each row and within each column of a 2D NumPy array 有没有一种方法可以在2D numpy数组中选择一行,且该行中的每一列都有条件? - Is there a way to select a row in an 2D numpy array with conditions for each column in the row? 如何使用矢量化在指定元素的同一行或列中随机选择 2D numpy 数组中的元素? - How to randomly select an element in a 2D numpy array in the same row or column of specified element using vectorization? 对于2d numpy数组的每一行,在第二个2d数组中获取相等行的索引 - For each row of a 2d numpy array get the index of an equal row in a second 2d array 修改2D NumPy数组的每一行中的不同列 - Modify different columns in each row of a 2D NumPy array 计算 2D numpy 数组每行的重复数 - count number of duplicates for each row of a 2D numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM