1

I have the following dataframe:

import pandas as pd

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})
df

dataframe

How do I center-align both the column titles/headers and the values in a dataframe, and how do I drop the index (the column with the values 0 and 1) in a dataframe?

Oleg
  • 560
  • 8
  • 19
Leockl
  • 1,906
  • 5
  • 18
  • 51
  • 1
    In order to drop the index you have to set a different index, unless you want to print it or save to a file, then you can set the relevant argument to `False` Regarding the alignment, where are you trying to align it? – Oleg Apr 22 '20 at 07:13
  • Hi @Oleg, I am wanting to align the headers and values to be in the center within each of it's respective "box" – Leockl Apr 22 '20 at 07:33
  • I found this `df.style.set_properties(**{'text-align': 'center'})` which would center-align the values, but how do I center-align the headers as well? – Leockl Apr 22 '20 at 07:39
  • 1
    It sounds that what you want is to edit the alignment settings of the notebook you're using When you're writing `df` it's similar to writing `display(df)` as it's mentioned here https://stackoverflow.com/a/29665452/8219391 Also you should check out pandas documentation on this: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Table-styles – Oleg Apr 22 '20 at 07:42
  • 1
    Ok many thanks @Oleg. I actually found an answer to this and have posted it below. – Leockl Apr 22 '20 at 08:15

4 Answers4

2

Found an answer for this. This should do the trick to center-align both headers and values and hiding the index:

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df1.set_properties(**{'text-align': 'center'}).hide_index()
Leockl
  • 1,906
  • 5
  • 18
  • 51
  • This works but the result is not a dataframe and Im not sure what it is (Styler?). How do we do the same thing but output a dataframe? – big_soapy Jan 22 '21 at 21:41
1

Try IPython.display

from IPython.display import HTML
HTML(df.to_html(index=False))

enter image description here

Sujit Dhamale
  • 1,311
  • 11
  • 14
  • Thanks @Sujit Dhamale. What about center-aligning the headers and values to be in the center within each of it's respective "box" in the dataframe? – Leockl Apr 22 '20 at 07:34
  • 2
    I found this `df.style.set_properties(**{'text-align': 'center'})` which would center-align the values, but how do I center-align the headers as well? – Leockl Apr 22 '20 at 07:39
1

Just to clarify the objective - what you want is to modify the display of the dataframe, not the dataframe itself. Of course, in the context of Jupyter, you may not care about the distinction - for example, if the only point of having the dataframe is to display it - but it's helpful to distinguish these things so you can use the right tools for the right thing.

In this case, as you've discovered, the styler gives you control over most aspects of the display of the dataframe - it does that by outputting and rendering html.

So if your dataframe 'looks' like this in Jupyter:

enter image description here

But you want it to look more like this:

enter image description here

you can use the styler to apply any number of styles (you chain them), so that styled HTML is rendered.

df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])\
        .hide(axis='index')

In Jupyter this will display as I've shown above, but if you wanted to display this elsewhere and wanted the underlying HTML (let's say you're rendering a page in Flask based on this), you can use the .to_html() method, like so:

enter image description here

There are two essential advantages to working in this way:

  1. The data in the dataframe remains in its original state - you haven't changed datatypes or content in anyway
  2. The styler opens up a vast array of tools to make your output look exactly the way you want.

In the context of Jupyter and numbers, this is particularly helpful because you don't have to modify the numbers (e.g. with rounding or string conversion), you just apply a style format and avoid exponential notation when you don't want it.

Here's a modified example, which shows how easy it is to use the styler to 'fix' the Jupyter Pandas numeric display problem:

enter image description here

James_SO
  • 1,169
  • 5
  • 11
0
df.style.set_properties(subset=["Feature", "Value"], **{'text-align': 'center'})