15

I have read this, but I am still confused about how I set the column width when using pandas.DataFrame.to_html.

import datetime
import pandas

data = {'Los Angles': {datetime.date(2018, 9, 24): 20.5, datetime.date(2018, 9, 25): 1517.1},
        'London': {datetime.date(2018, 9, 24): 0, datetime.date(2018, 9, 25): 1767.4},
        'Kansas City': {datetime.date(2018, 9, 24): 10, datetime.date(2018, 9, 25): 1.4}}

df = pandas.DataFrame(data=data)

html = df.to_html()

The above code results in : df.to_html result

How do I go about forcing the column width so that they are the same?

4 Answers 4

28

Try this:

import datetime
import pandas

data = {'Los Angles': {datetime.date(2018, 9, 24): 20.5, datetime.date(2018, 9, 25): 1517.1},
        'London': {datetime.date(2018, 9, 24): 0, datetime.date(2018, 9, 25): 1767.4},
        'Kansas City': {datetime.date(2018, 9, 24): 10, datetime.date(2018, 9, 25): 1.4}}

df = pandas.DataFrame(data=data)
pandas.set_option('display.max_colwidth', 40)

html = df.to_html()
Sign up to request clarification or add additional context in comments.

2 Comments

I have edited the answer, see if it works. Also you can read more here and here
the max_colwidth property doesn't help to set the minimum column width, which is often an issue for columns that wrap around. Is there anyway to fix that? Thank you. I have read the link you have above and it doesn't address this at all.
4

I find it better to use the set_table_attributes function. You are then able to set e.g. a CSS-class to the table. In case you have many tables on your output it is much better readable.

import datetime
import pandas

data = {'Los Angles': {datetime.date(2018, 9, 24): 20.5, datetime.date(2018, 9, 25): 1517.1},
        'London': {datetime.date(2018, 9, 24): 0, datetime.date(2018, 9, 25): 1767.4},
        'Kansas City': {datetime.date(2018, 9, 24): 10, datetime.date(2018, 9, 25): 1.4}}

df = pandas.DataFrame(data=data)

html = df.set_table_attributes('class="table-style"').to_html()

Your CSS could look something like that:

    .table-style {
                  width: 100%;
}

    .table-style td {
                  width: 100px;
}

2 Comments

Hi @bender, may I know where should I place the .table-style in the code?
Hey, your CSS goes either in a css-file (which is e.g. called style.css) and is linked in the head-section of your HTML file or you can place your css directly in the head-section of the HTML file. The former is cleaner. I normally use first the latter version and when things get complicated/huge switch to the cleaner version with CSS code in a different file. Here's a description of how to implement CSS: w3schools.com/Css/css_howto.asp
2

Another option is

df.style.set_table_styles([dict(selector='th', props='min-width: 60px;'),])

Comments

1

You can now set the col_space argument of to_html. col_space specifies the min width of all the columns.

df.to_html(col_space='75px')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.