Use Pandas index in Plotly Express Use Pandas index in Plotly Express python python

Use Pandas index in Plotly Express


Reference: https://plot.ly/python/px-arguments/#using-the-index-of-a-dataframe

You can pass the index as reference explicitly.

So in your case, this would be:

import plotly.express as pxiris = px.data.iris()fig = px.scatter(iris, x=iris.index, y="sepal_length")fig.show()

--

BONUS QUESTION: what if iris has a pd.MultiIndex?

Use pd.MultiIndex.get_level_values.

import plotly.express as px# dummy example for multiindexiris = px.data.iris().set_index(['species', 'species_id', iris.index])fig = px.scatter(   iris,    x=iris.index.get_level_values(2),    y="sepal_length")fig.show()


You can simply leave it blank, like so:

import plotly.express as pxiris = px.data.iris()fig = px.scatter(iris, y="sepal_length")fig.show()


You can only pass the column name at x & y parameters of px.scatter(). Seems there is no column named "index" in the iris dataset.