Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Pandas to_html() truncates string contents

I have a Python Pandas DataFrame object containing textual data. My problem is, that when I use to_html() function, it truncates the strings in the output.

For example:

import pandas
df = pandas.DataFrame({'text': ['Lorem ipsum dolor sit amet, consectetur adipiscing elit.']})
print (df.to_html())

The output is truncated at adapis...

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>text</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> Lorem ipsum dolor sit amet, consectetur adipis...</td>
    </tr>
  </tbody>
</table>

There is a related question on SO, but it uses placeholders and search/replace functionality to postprocess the HTML, which I would like to avoid:

Is there a simpler solution to this problem? I could not find anything related from the documentation.

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

What you are seeing is pandas truncating the output for display purposes only.

The default max_colwidth value is 50 which is what you are seeing.

You can set this value to whatever you desire or you can set it to -1 which effectively turns this off:

pd.set_option('display.max_colwidth', -1)

Although I would advise against this, it would be better to set it to something that can be displayed easily in your console or ipython.

A list of the options can be found here: http://pandas.pydata.org/pandas-docs/stable/options.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...