Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add table title in python preferably with pandas

I would like to add a table title in the out put. It's relatively easy to do this in r with flextable, you just have to use function set_caption(), is there a similar package in python with pandas dataframe (maybe PTable)?

This is what I have right now, pic1

and this is what I would like to have pic2

Please help, thanks guys!

like image 791
timxymo1225 Avatar asked Sep 16 '19 13:09

timxymo1225


People also ask

How do I add a title to a panda table?

You can add header to pandas dataframe using the df. colums = ['Column_Name1', 'column_Name_2'] method. You can use the below code snippet to set column headers to the dataframe.

How do I set a header row in pandas?

We can create a data frame of specific number of rows and columns by first creating a multi -dimensional array and then converting it into a data frame by the pandas. DataFrame() method. The columns argument is used to specify the row header or the column names.

How do you add a name to a DataFrame in Python?

Add Cloumn Names to DataFrame You can add column names to pandas DataFrame while creating manually from the data object. In order to create a DataFrame, you would use a DataFrame constructor which takes a columns param to assign the names.

How do you give pandas a series name?

rename() function is used to alter Series index labels or name for the given Series object. inplace : Whether to return a new Series. If True then value of copy is ignored. level : In case of a MultiIndex, only rename labels in the specified level.


3 Answers

Have you tried this doing this?

df.style.set_caption("Hello World")

Source: Pandas Styling

EDIT:

Here's an alternative way to present your table if you're okay with using matplotlib

import matplotlib.pyplot as plt
import pandas as pd

my_frame = pd.DataFrame(data={'simulation1':[71,4.8,65,4.7],
                              'simulation2':[71,4.8,69,4.7],
                              'simulation3':[70,3.8,68,4.9],
                              'experiment':[70.3,3.5,65,4.4]})
#my_frame Display pandas table

fig = plt.figure(figsize = (8, 2))
ax = fig.add_subplot(111)

ax.table(cellText = my_frame.values,
          rowLabels = my_frame.index,
          colLabels = my_frame.columns,
          loc = "center"
         )
ax.set_title("Top 10 Fields of Research by Aggregated Funding Amount")

ax.axis("off");
like image 65
Maku Avatar answered Oct 22 '22 06:10

Maku


It appears this does not set the caption 'inplace', like:

df.reset_index(drop=True, inplace=True)

is the same as:

df = df.reset_index(drop=True)

Instead try:

df = df.style.set_caption('Top 10 Fields of Research by Aggregated Funding Amount')
like image 34
PaulR Avatar answered Oct 22 '22 08:10

PaulR


Try this

df.style.set_table_attributes("style='display:inline'").set_caption('Caption table')

The output, with the title (hi-lighted in yellow) that will be displayed, is shown below

enter image description here

like image 6
Rahul Kumeriya Avatar answered Oct 22 '22 08:10

Rahul Kumeriya