简体   繁体   English

3 Arrays (X,Y,Z) 到 2 轴 (X,Y) 和网格 (Z)

[英]3 Arrays (X,Y,Z) to 2 Axis (X,Y) and Grid (Z)

I repeating the same step on several results from my data.我对我的数据中的几个结果重复相同的步骤。 I am looking to convert my arrays into a lookup table:我希望将我的 arrays 转换为查找表:

import pandas as pd


x = np.array(
    [
        1.0,1.0,1.0,1.0,1.0,2.0,2.0,2.0,2.0,2.0,3.0,3.0,3.0,3.0,3.0,4.0,4.0,4.0,4.0,4.0,5.0,5.0,5.0,5.0,5.0
    ]
)
y = np.array(
    [
        10.0,20.0,30.0,40.0,50.0,10.0,20.0,30.0,40.0,50.0,10.0,20.0,30.0,40.0,50.0,10.0,20.0,30.0,40.0,50.0,10.0,20.0,30.0,40.0,50.0
    ]
)
z = np.random.rand(25)

data = {"x": x, "y": y, "z": z}
print(data)

Results in:结果是:

      x     y         z
0   1.0  10.0  0.822260
1   1.0  20.0  0.521899
2   1.0  30.0  0.064821
3   1.0  40.0  0.853616
4   1.0  50.0  0.944020
5   2.0  10.0  0.055000
6   2.0  20.0  0.328647
7   2.0  30.0  0.949762
8   2.0  40.0  0.904646
9   2.0  50.0  0.927425
10  3.0  10.0  0.738802
11  3.0  20.0  0.508795
12  3.0  30.0  0.246913
13  3.0  40.0  0.810238
14  3.0  50.0  0.340421
15  4.0  10.0  0.274144
16  4.0  20.0  0.738095
17  4.0  30.0  0.255514
18  4.0  40.0  0.784286
19  4.0  50.0  0.114887
20  5.0  10.0  0.443540
21  5.0  20.0  0.691474
22  5.0  30.0  0.311146
23  5.0  40.0  0.557778
24  5.0  50.0  0.670089

I would like to format / shape it in a way so I get the following:我想以某种方式格式化/塑造它,所以我得到以下信息:

    1           2           3           4           5
10  0.82226     0.055       0.738802    0.274144    0.44354
20  0.521899    0.328647    0.508795    0.738095    0.691474
30  0.064821    0.949762    0.246913    0.255514    0.311146
40  0.853616    0.904646    0.810238    0.784286    0.557778
50  0.94402     0.927425    0.340421    0.114887    0.670089

One way guess I could it is doing a for loop and looking at the unique values, but I was wondering if there is a better/obvious way to do this.一种方式猜测我可以做一个 for 循环并查看唯一值,但我想知道是否有更好/明显的方法来做到这一点。

Today I learned what a pivot table is..今天我了解了 pivot 表是什么..

Using the pandas module:使用 pandas 模块:

df = pd.Dataframe{data}
print(df.pivot(index="y", columns="x", values="z"))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM