简体   繁体   English

如何将 3-D Numpy 数组转换为 Pandas Dataframe?

[英]How to convert 3-D Numpy array to Pandas Dataframe?

The problem : I have a 3-D Numpy Array:问题:我有一个 3-D Numpy 数组:

X

X.shape: (1797, 2, 500)

z=X[..., -1]
print(len(z))
print(z.shape)
count = 0
for bot in z:
    print(bot)
    count+=1
    if count == 3: break

Above code yields following output:上面的代码产生以下输出:

1797
(1797, 2)
[23.293915 36.37388 ]
[21.594519 32.874397]
[27.29872  26.798382]

So, there are 1797 data points - each with a X and a Y coordinate and, there are 500 iterations of these 1797 points.因此,有 1797 个数据点 - 每个都有一个 X 和一个 Y 坐标,并且这 1797 个点有 500 次迭代。

I want a DataFrame such that:我想要一个数据帧,这样:

Index Column       |  X-coordinate  |  Y-coordinate
0                  |  X[0][0][0]    |  X[0][1][0]
0                  |  X[1][0][0]    |  X[1][1][0]
0                  |  X[2][0][0]    |  X[2][1][0]
('0') 1797 times
1                  |  X[0][0][1]    |  X[0][1][1]
1                  |  X[1][0][1]    |  X[1][1][1]
1                  |  X[2][0][1]    |  X[2][1][1]
('1' 1797 times)
.
.
.
and so on
till 500

I tried techniques mentioned here, but numpy/pandas is really escaping me:我尝试了这里提到的技术,但 numpy/pandas 真的让我望而却步:

  1. How To Convert a 3D Array To a Dataframe 如何将 3D 数组转换为数据帧
  2. How to transform a 3d arrays into a dataframe in python 如何在python中将3d数组转换为数据帧
  3. Convert numpy array to pandas dataframe 将 numpy 数组转换为 Pandas 数据框
  4. easy multidimensional numpy ndarray to pandas dataframe method? 简单的多维numpy ndarray到pandas数据帧方法?
  5. numpy rollaxis - how exactly does it work? numpy rollaxis - 它究竟是如何工作的?

Please help me out.请帮帮我。 Hope I am adhering to the question-asking discipline.希望我遵守提问纪律。

Here's a solution with sample data:这是带有示例数据的解决方案:

a,b,c = X.shape
# in your case
# a,b,c = 1797, 500

pd.DataFrame(X.transpose(1,2,0).reshape(2,-1).T,
             index=np.repeat(np.arange(c),a),
             columns=['X_coord','Y_coord'] 
            )

Output:输出:

   X_coord  Y_coord
0        0        3
0        6        9
0       12       15
0       18       21
1        1        4
1        7       10
1       13       16
1       19       22
2        2        5
2        8       11
2       14       17
2       20       23

Try this way:试试这个方法:

index = np.concatenate([np.repeat([i], 1797) for i in range(500)])
df = pd.DataFrame(index=index)
df['X-coordinate'] = X[:, 0, :].T.reshape((-1))
df['Y-coordinate'] = X[:, 1, :].T.reshape((-1))

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

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