0

enter image description here

I am trying to make a table like one above in plotly with column headers and row headers. I have the headers as lists x = [a,b,c,d] y =[red,wite,blue] and the values as nested lists of rows z=[[5,8,9,5],[4,4,5,2],[2,5,3,1]].

The code I have for he table:

fig_table = go.Table(
        header=dict(
            values=x,
            font=dict(size=10),
            align="left"
        ),
        cells=dict(
            values=z,
            align = "left")
    )

The values property expects a list of columns instead of rows so my table comes out inverted. Is there a way to enter a list of rows into values? Also how would I add row headers? Thanks

1 Answer 1

1
  • the structure for the header is a 1D list, structure for values is a 2D list which is a transpose of a 2D list you will find in pandas
  • have coded both examples of constructing from lists and dataframe

create from lists

go.Figure(go.Table(
        header=dict(
            values=[""] + list("abcd"),
            font=dict(size=10),
            align="left"
        ),
        cells=dict(
            values=[["red","white","blue"],[5,3,2],[8,4,5],[9,5,3],[5,2,1]],
            align = "left")
    ))

create from a dataframe

df = pd.DataFrame(
    index=["red", "white", "blue"],
    columns=list("abcd"),
    data=np.array([[5, 3, 2], [8, 4, 5], [9, 5, 3], [5, 2, 1]]).T,
)

go.Figure(go.Table(header={"values":df.reset_index().columns, "font":{"size":10}, "align":"left"},
                  cells={"values":df.reset_index().T, "align":"left"}))
Sign up to request clarification or add additional context in comments.

1 Comment

The table I have is large (about 50 columns), I just used this one as an example so I was looking for way create a table like one above keeping the inputs x,y and z the same format as shown, and setting x as column header and y as row header. For z would i have to make a function to transpose it to a list of columns?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.