{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyMx4i9Yp1FHKGbbq6Zy/fEO"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Tables\n","\n","`plotly.express` does not include a table function, but we can create a graph object table using `go.Figure()` in combination with `go.Table()`. We can create two columns of data with sample scores for `A` and `B` letter grades."],"metadata":{"id":"B__IlWeYfxNn"}},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"iOHJhBCLfuYk","executionInfo":{"status":"ok","timestamp":1706150560986,"user_tz":300,"elapsed":596,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"bded726b-6499-4c14-bb01-9c541d2ca9d1"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["import plotly.graph_objects as go # import statement\n","# create figure with data\n","fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),\n"," cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))\n"," ])\n","# show figure\n","fig.show()"]},{"cell_type":"markdown","source":["There's a lot to get into in terms of the differences between `plotly.express` function syntax and `plotly.graph_object` syntax.\n","\n","For our purposes, we can focus on how the table header takes a dictionary with column labels, and the cells also take a dictionary with two lists of values. These dictionaries are passed to `go.Figure()` and `go.Table()` to create the plot."],"metadata":{"id":"skeH4eDIf3Kx"}},{"cell_type":"markdown","source":["## DataFrames & Tables\n","\n","We could also generate a table from data stored in a `pandas` `DataFrame`.\n","\n","This example also includes style parameters for the table."],"metadata":{"id":"s5d1Nt6Df82F"}},{"cell_type":"code","source":["import pandas as pd # import statement\n","df = pd.read_csv('https://raw.githubusercontent.com/kwaldenphd/elements-of-computing/main/book/data/ch11/2014_usa_states.csv') # load data\n","df # inspect data"],"metadata":{"id":"A77kVnNKgAPt"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# create figure\n","fig = go.Figure(data=[go.Table(\n"," header=dict(values=list(df.columns),\n"," fill_color='paleturquoise',\n"," align='left'),\n"," cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],\n"," fill_color='lavender',\n"," align='left'))\n","])\n","\n","# show figure\n","fig.show()"],"metadata":{"id":"S1P8pSd8gGfx"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["This example includes style attributes like `fill_color` and `align` for both `header` and `cells`.\n","\n","For `header`, this example takes a dictionary with the `DataFrame` column labels as a list, and the `DataFrame` column values as values for the `cells`."],"metadata":{"id":"1WLOjgxSgI3L"}},{"cell_type":"markdown","source":["## Additional Resources\n","\n","- [`plotly`, Tables in Python](https://plotly.com/python/table/)\n","- [`plotly`, Python Figure Reference: `table` Traces](https://plotly.com/python/reference/table/)"],"metadata":{"id":"AqU-H7-JgLc_"}}]}