12

I'm trying to convert a matrix into a pandas data frame:

matrixA={}
matrixA[0,0]='a'
matrixA[0,1]='b'
matrixA[1,0]='c'
matrixA[1,1]='d'

Like this:

import pandas as pd

pd.DataFrame(matrixA)

I get an error.

Hangon
  • 2,449
  • 7
  • 23
  • 31

2 Answers2

24

As already said your are not creating a matrix but a python dictionary. However a dict can serve as parameter to create a dataframe, but you reversed the indexing order.

import pandas as pd

matrixA={}
matrixA['a']=[0,0]
matrixA['b']=[0,1]

pd.DataFrame(matrixA)

   a  b
0  0  0
1  0  1

Additionally you can use numpys matrix

import numpy as np

a = np.matrix('1 2; 3 4')
pd.DataFrame(a)

   0  1
0  1  2
1  3  4
greole
  • 4,523
  • 5
  • 29
  • 49
  • I used this [link](http://stackoverflow.com/questions/6667201/how-to-define-two-dimensional-array-in-python) "If all you want is a two dimensional container to hold some elements, you could conveniently use a dictionary instead: 'Matrix = {}' Then you can do: 'Matrix[1,2] = 15' 'print Matrix[1,2]' This works because 1,2 is a tuple, and you're using it as a key to index the dictionary. The result is similar to a dumb sparse matrix." So now I would like to save the data I saved that way in DataFrame It is feasible to use a build in function? – Hangon Nov 06 '14 at 12:40
  • Well the answer you refer to, is a bit out of the context of what you are trying to do. Pandas DataFrame init can't handle a dictionary like that (as the error you should recieve indicates) and besides that I don't think it is a really pythonic way to use a dict like that. – greole Nov 06 '14 at 12:49
  • Thanks greole for the help! I will try to run away from this way of using "dict" next time. Now i will pass my values of this "dict" using 2 loops to the DataFrame. Best regards – Hangon Nov 06 '14 at 12:53
2

Well, I was wondering if we could use python's multi-dimensional array. Yes, you can use python matrix (as mentioned in the python official docs) or multi-dimensional arrays and convert into pandas DataFrame.

import pandas as pd
matrix = [
    ["a", 1],
    ["b", 2]
]
pd.DataFrame(matrix)

   0  1
0  a  1
1  b  2
mythicalcoder
  • 3,143
  • 1
  • 32
  • 42