bismo
bismo

Reputation: 1439

How to add a title to a pandas dataframe

I've been diving deep into pandas styler options and have added images, conditional formatting, and a caption to my df. Now, I would like to finish it off with a title. I can't seem to figure out how to do that though. In the style documentation the example it gives turns the df into an HTML table in order to give it a title, but I don't want to do that. This SO question helped me add my caption, but not a title. Is there any way to do this? Thanks!

styles = [
    dict(selector="caption", props=[("caption-side", "bottom")]),
    dict(selector='th', props=[('text-align', 'center')])
]

Here is some of my style code. Is there a selector for "title"?

Upvotes: 3

Views: 6398

Answers (1)

Ravishankar Sharma
Ravishankar Sharma

Reputation: 13

Access the index of a DataFrame with pandas.DataFrame.index. Assign a title to the name attribute of the index.

df = pd.DataFrame({"Letters": ["a", "b", "c"], "Numbers": [1, 2, 3]})
index = df.index
index.name = "Index Title"

print(df)

Upvotes: 1

Related Questions