Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - Use Pandas index in Plotly Express

Plotly Express allows me to easily plot a pandas dataframe, as explained in their examples. Rather than using a named column for x and a named column for y, I would like to use the dataframe's index for x and a named column for y.

Example using named columns

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

What i want (bogus example)

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

This obviously throws:

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species', 'species_id'] but received: index

Ugly fix

import plotly.express as px
iris = px.data.iris().reset_index()
fig = px.scatter(iris, x="index", y="sepal_length")
fig.show()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 px
iris = 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 multiindex
iris = 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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...