2017-04-08 140 views
0

我想將矩陣轉換爲3D數組,或將3D數組轉換爲矩陣。如何輸入數據以及如何在Python中完成轉換工作?如何將矩陣轉換爲3D數組或反之亦然?

我找了很多地方,但沒有答案。請幫我

矩陣:

a b c 
d 1 2 3 
e 2 3 4 
f 4 3 2 

數組b:

a d 1 
a e 2 
a f 4 
b d 2 
b e 3 
b f 3 
c d 3 
c e 4 
c f 2 

我可以用堆棧()來實現我的目標是什麼?

喜歡:Python pandas - pd.melt a dataframe with datetime index results in NaN

回答

0

所以你的數據不是真正三維的,但2維。你本質上是試圖去除你的2d數據。這通常被稱爲melt。您最好的選擇是將數據加載到pandas數據幀中。

import pandas as pd 
df = pd.DataFrame([['d',1,2,3],['e',2,3,4],['f',4,3,2]], columns=['idx','a','b','c']) 

df 
# returns: 
    idx a b c 
0 d 1 2 3 
1 e 2 3 4 
2 f 4 3 2 

pd.melt(df, id_vars='index', value_vars=list('abc')) 
# returns: 
    idx variable value 
0 d  a  1 
1 e  a  2 
2 f  a  4 
3 d  b  2 
4 e  b  3 
5 f  b  3 
6 d  c  3 
7 e  c  4 
8 f  c  2 
+0

謝謝你這麼多的具體的答案!但是從pd.melt得到的回報idx全部是NaN,你能檢查它嗎? – yfxia

+0

我可以使用堆棧()來實現我的目標嗎?像http://stackoverflow.com/questions/30984167/python-pandas-pd-melt-a-dataframe-with-datetime-index-results-in-nan – yfxia

0

我不是很熟悉的大熊貓庫,但這裏是用Python標準庫粗略的解決方案:

#!/usr/bin/env python2 
""" 
Convert a matrix to 2D arrays and vice versa 
http://stackoverflow.com/questions/43289673 
""" 

from collections import OrderedDict 


TEST_MATRIX = """\ 
    a b c 
d 1 2 3 
e 2 3 4 
f 4 3 2 
""" 


def parse_matrix(matrix_string): 
    """Parse a matrix string and return list of tuples representing data""" 
    matrix_string = matrix_string.strip() 
    list_of_lines = matrix_string.splitlines() 
    parsed_list = [] 
    y_headers = list_of_lines[0].split() 
    data_rows = [i.split() for i in list_of_lines[1:]] 
    for y in y_headers: 
     for row in data_rows: 
      parsed_list.append((y, row[0], row[y_headers.index(y) + 1])) 
    return parsed_list 


def convert_to_matrix(data): 
    """ 
    Convert a parsed matrix (in the form of a list of tuples) to a matrix 
    (string) 
    """ 
    # Messes up ordering 
    # y_headers = set(i[0] for i in data) 
    # x_headers = set(i[1] for i in data) 

    y_headers = OrderedDict() 
    x_headers = OrderedDict() 
    [(y_headers.setdefault(i[0]), x_headers.setdefault(i[1])) for i in data] 

    matrix_string = " " + " ".join(y_headers) # header 
    for x in x_headers: 
     row = [x] 
     for y in y_headers: 
      val = [i[-1] for i in data if i[0] == y and i[1] == x][0] 
      row.append(val) 
     row_string = " ".join(row) 
     matrix_string += "\n" + row_string 
    return matrix_string 


def main(): 
    print("Test matrix:") 
    print(TEST_MATRIX) 

    # parse the test matrix string to a list of tuples 
    parsed_test_matrix = parse_matrix(TEST_MATRIX) 
    # print the parsed matrix 
    print("Parsed matrix:") 
    for row in parsed_test_matrix: 
     print " ".join(row) 
    print 

    # convert parsed matrix back to the original matrix and print 
    print("Convert parsed matrix back to matrix:") 
    print(convert_to_matrix(parsed_test_matrix)) 


if __name__ == "__main__": 
    main() 
相關問題