将坐标打印到带有数据的 csv 文件
Printing Coordinates to a csv File with Data
我需要将坐标和关联数据提取到 .csv 文件中。
我有一个 4x4 矩阵,写成列表 (network1) 以及每个索引的相应值 (q_val)。我的目标是确定 1 在 network1 中出现的坐标,并将这些坐标与相应的 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,2)、(2,3)、(3,3)、(3,4)、(4,3)、(4,4)]。
- 将这些打印到 .csv 文件中,不带方括号和逗号,仅以空格分隔。这应该看起来像下面的照片。
- 理想情况下,我还希望它在页眉和 ; 中打印 q_val。低于结果。
非常感谢任何帮助 - 我相信还有更有效的方法来完成这个过程,所以我非常乐意接受任何建议!
你可以试试下面的代码我有 added/changed 几行你的原始代码,我已经评论过:
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)
如您所见,我在页眉中添加了两个标签。希望没关系
这应该能满足您的要求:
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)
我需要将坐标和关联数据提取到 .csv 文件中。
我有一个 4x4 矩阵,写成列表 (network1) 以及每个索引的相应值 (q_val)。我的目标是确定 1 在 network1 中出现的坐标,并将这些坐标与相应的 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,2)、(2,3)、(3,3)、(3,4)、(4,3)、(4,4)]。
- 将这些打印到 .csv 文件中,不带方括号和逗号,仅以空格分隔。这应该看起来像下面的照片。
- 理想情况下,我还希望它在页眉和 ; 中打印 q_val。低于结果。
非常感谢任何帮助 - 我相信还有更有效的方法来完成这个过程,所以我非常乐意接受任何建议!
你可以试试下面的代码我有 added/changed 几行你的原始代码,我已经评论过:
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)
如您所见,我在页眉中添加了两个标签。希望没关系
这应该能满足您的要求:
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)