Home > Blockchain >  How to select automatically specific columns from df.columns for table on plotly.go.Table
How to select automatically specific columns from df.columns for table on plotly.go.Table

Time:10-19

I am creating a table which is a plotly go.Figure() object and I am trying to display only a few columns headers from my df.

fig_positions = go.Figure(data=go.Table(
            header=dict(values=[df.columns[['Block Number', 'Base Token']]]
)))

However, I'm getting the following error:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

If I use df.columns instead, it creates the table with all df columns.

How can I create a table with plotly.go.Figure() wit selected column headers without manually specifying each one like (df['Block Number'], df['Base Token'], ...)

CodePudding user response:

There are header and cells and you should define each one as follows:

fig = go.Figure(data=[go.Table(
    header=dict(values=list(df.columns)),
    cells=dict(values=[df.col1, df.col2, df.col3, df.col4])
])

You can create your list automatically with specific columns as follows:

headers=dict(values=[df.columns[col] for col in [1,3,4]])
cells=dict(values=[df.iloc[:,col] for col in [1,3,4]])

Here I only create a table with columns indexed by 1, 3, and 4.

  • Related