XuM*_*MuK 1 python matrix python-2.7 export-to-csv
我有格式列表:
[[x_0, y_0, z_0], [x_1, y_1, z_1] ... [x_n, y_n, z_n]]
Run Code Online (Sandbox Code Playgroud)
其中 x_0 == x_1:
[[0, 0, z_0], [0, 1, z_1], [0, 2, z_2]... [1, 0, z_o] ... [x_n, y_n, z_n]]
Run Code Online (Sandbox Code Playgroud)
我想将其转换为这样的矩阵(最好创建 csv 文件):
主要问题 - 我不知道数组的总大小(它每次都会改变,但它总是方形的)。进行这种转变的最简单方法是什么?
该列表是点的 XYZ 坐标。
使用 pandas 创建数据透视表。数据透视表允许您以行格式获取数据并将其重新排列为行、列和值。
import pandas as pd
import random
# create some fake data
data = [(i,j,0.1*random.randrange(1,50,1)) for i in range(5) for j in range(5)]
# [(0, 0, 4.2), (0, 1, 1.9), (0, 2, 1.2), (0, 3, 1.2), (0, 4, 1.6),
# (1, 0, 2.0), (1, 1, 0.9), (1, 2, 3.5), (1, 3, 3.0), (1, 4, 0.8),
# (2, 0, 4.9), (2, 1, 2.8), (2, 2, 1.8), (2, 3, 2.7), (2, 4, 0.2),
# (3, 0, 3.0), (3, 1, 1.8), (3, 2, 0.3, (3, 3, 3.3), (3, 4, 4.4),
# (4, 0, 1.9), (4, 1, 4.5), (4, 2, 3.6), (4, 3, 0.4), (4, 4, 0.4)]
# read it into a dataframe:
df = pd.DataFrame(data, columns=['x','y','z'])
# create pivot table with x and rows, y and columns, z as values
df.pivot_table(values='z', index='x', columns='y')
# y 0 1 2 3 4
# x
# 0 4.2 1.9 1.2 1.2 1.6
# 1 2.0 0.9 3.5 3.0 0.8
# 2 4.9 2.8 1.8 2.7 0.2
# 3 3.0 1.8 0.3 3.3 4.4
# 4 1.9 4.5 3.6 0.4 0.4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9447 次 |
| 最近记录: |