[英]Printing Coordinates to a csv File with Data
I need to extract coordinates with associated data to a .csv file.我需要将带有关联数据的坐标提取到 .csv 文件中。
I have a 4x4 matrix written as a list (network1) as well as corresponding values for each index (q_val).我有一个 4x4 矩阵写为列表 (network1) 以及每个索引 (q_val) 的相应值。 My goal is to identify the coordinates where a 1 occurs within network1, and export these coordinates along with the corresponding q_val to a .csv file.
我的目标是确定 network1 中出现 1 的坐标,并将这些坐标与相应的 q_val 一起导出到 .csv 文件。
Please see the current code below:请参阅下面的当前代码:
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)
My Python output from this process looks correct and is as follows:我从这个过程中得到的 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)
It would be great to get some assistance to complete the following additional steps:获得一些帮助以完成以下附加步骤会很棒:
I greatly appreciate any help - I'm sure there are more efficient methods of completing this process as well, so I am more than happy to take on board any suggestions!我非常感谢任何帮助 - 我相信还有更有效的方法来完成这个过程,所以我很乐意接受任何建议!
This should satisfy your requirements:这应该满足您的要求:
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)
You can try the following code I have added/changed a few lines to your original code which I have commented:你可以试试下面的代码,我在你的原始代码中添加/更改了几行,我已经注释了:
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)
As you can see I have added two more labels in the header.如您所见,我在标题中又添加了两个标签。 Hope that's okay
希望没关系
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.