Linked Questions

Popular Questions

I have a dictionary whose keys are tuples of the form (i,j) and whose values are matrix entries.

So if you think of a mathematical matrix $A = (a_{i,j})$ then matrix_dict[(i,j)] would give the value of row i and column j.

I would like to have a pandas dataframe where the values of matrix_dict[(i,0)] for i in range 1 to m+1 are the names of the rows, matrix_dict[(0,j)] for j in range 1 to n+1 the names of the columns and all values where none of the tuple indices (i,j) are 0 to be the entries of the df with the corresponding row and column index.

The dictionary would look like this:

matrix_dict = {
    (0, 0): 'RowIndex\ColumnIndex',
    (0, 1): 'Column1',
    (0, 2): 'Column2',
    (1, 0): 'Row1',
    (1, 1): 1,
    (1, 2): 2,
    (2, 0): 'Row2',
    (2, 1): 3,
    (2, 2): 4

I thought it would be easy to convert that into a pandas dataframe as the structure already matches in a way, but the solutions I found on here using pd.DataFrame.from_dict are for different problems where the key tuple is supposed to become part of the dataframe or multi-indices.

Related Questions