【问题标题】:Printing Coordinates to a csv File with Data将坐标打印到带有数据的 csv 文件
【发布时间】:2021-01-27 13:17:36
【问题描述】:

我需要将坐标与相关数据提取到 .csv 文件中。

我有一个 4x4 矩阵写成一个列表 (network1) 以及每个索引 (q_val) 的相应值。我的目标是确定 network1 中出现 1 的坐标,并将这些坐标与相应的 q_val 一起导出到 .csv 文件。

请看下面的当前代码:

network1 = [0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,1]
q_val = [50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800]

data = np.array(network1)
shape = (4,4)
network2 = data.reshape(shape)
print(network2)

coordinates = np.where(network2 == 1)
print(coordinates)

listOfCoordinates= list(zip(coordinates[0], coordinates[1]))

for coords in listOfCoordinates:      
    print(coords)

我从这个过程中得到的 Python 输出看起来是正确的,如下所示:

[[0 1 0 0]
 [0 0 1 0]
 [0 0 1 1]
 [0 0 1 1]]

(array([0, 1, 2, 2, 3, 3], dtype=int64), array([1, 2, 2, 3, 2, 3], dtype=int64))

(0, 1)
(1, 2)
(2, 2)
(2, 3)
(3, 2)
(3, 3)

如果能获得一些帮助来完成以下附加步骤,那就太好了:

  1. 将索引移动 +1 [即结果应该是 (1,2), (2,3), (3,3), (3,4), (4,3), (4,4)]。
  2. 将这些打印到 .csv 文件中,不带括号和逗号,仅由空格分隔。这应该如下图所示。
  3. 理想情况下,我还希望它在标题中使用 q_val 和 ;在结果下方。

非常感谢任何帮助 - 我确信还有更有效的方法可以完成此过程,因此我非常乐意接受任何建议!

【问题讨论】:

    标签: python arrays numpy coordinates export-to-csv


    【解决方案1】:

    这应该满足您的要求:

    import numpy as np
    import pandas as pd
    
    network1 = [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1]
    q_val = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800]
    
    network1_matrix = np.array(network1).reshape(4, 4)
    q_val_matrix = np.array(q_val).reshape(4, 4)
    
    
    coordinates = np.transpose(np.where(network1_matrix == 1))
    
    filtered_values = [q_val_matrix[tuple(coordinate)] for coordinate in coordinates]
    
    # Create the DataFrame and add 1 to each index
    result_df = pd.DataFrame({'1': coordinates[:, 0] + 1, '2': coordinates[:, 1] + 1, '3': filtered_values})
    # Add ';' in the end of the DataFrame
    result_df = result_df.append({'1': ';', '2': '', '3': ''}, ignore_index=True)
    # Add 'q_val' as header
    result_df.columns = ['q_val', '', '']
    # Export the DataFrame as csv file
    result_df.to_csv('result.csv', sep=' ', index=False)
    

    【讨论】:

    • 这似乎工作得很好 - 太棒了,谢谢!
    【解决方案2】:

    您可以尝试以下代码,我已在您的原始代码中添加/更改了几行,并已发表评论:

    import csv # import cvs
    
    network1 = [0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,1]
    q_val = [50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800]
    
    
    data = np.array(network1)
    shape = (4,4)
    network2 = data.reshape(shape)
    print( network2)
    
    coordinates = np.where(network2 == 1) 
    
    # select the needed q_val 
    q_val_selected = [q_val[i] for i,v in enumerate(network1) if network1[i]==1] # select the needed q_val 
    
    print(coordinates)
    
    # add "+1"s and the selected q_vals
    listOfCoordinates= list(zip(coordinates[0] + 1, coordinates[1] + 1, q_val_selected)) 
    
    
    for coords in listOfCoordinates:      
        print(coords)
    
    # add a header and semicolon
    listOfCoordinates = [("c1", "c2", "q_val")] + listOfCoordinates + [(";")]
        
    # wrte to a csv file
    with open('some.csv', 'w', newline='') as f:
        writer = csv.writer(f,delimiter=' ')
        writer.writerows(listOfCoordinates)
    
    

    如您所见,我在标题中添加了另外两个标签。希望没事

    【讨论】:

    • 非常感谢您将这些放在一起!它似乎也能按预期工作 - 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多