14

I need to display a pandas dataframe in a Stack Overflow question body. I have already tried to display copying and pasting in a block of code but got completely unreadable.

This is result I got:

                             id                            text  top_class  class_name  confidence
8988   20809_451291030_17944026    ainda nao recebi minha senha       1001        1001    0.976569
8989   20810_459751030_18018215    ainda nao recebi minha senha       1001        1001    0.976569
9009   20810_455341030_17968671        ainda nao recebi a senha       1001        1001    0.971450
9010   20810_455341030_17968671        ainda nao recebi a senha       1001       14008    0.011035
9011   20809_458715030_18010104        ainda nao recebi a senha       1001        1001    0.971450

I want to display my dataframe similar to the following.

Picture of the desired output

How can I achieve this result on a question?

5
  • 7
    That actually looks pretty good to me. Commented Dec 5, 2019 at 14:09
  • 2
    Do you wish to have the headers included or is the current behavior of no headers correct? Commented Dec 5, 2019 at 14:16
  • thanks for your time @QuangHoang and @Spencer Stream, yes i wanted the headers to be display also, and i could achive the desire result using language identifier on my block of code python Commented Dec 5, 2019 at 14:43
  • 1
    @Wallace You had a typo in the formatting of this post, where the header row was in the space where the language identifier should go.
    – wjandrea
    Commented Apr 5 at 1:33
  • Voting to close as unclear, because I can't understand how there is anything wrong with the described result - I certainly can't understand how it could be called "completely unreadable". Unless - checking the question edit history - the original problem was that OP simply didn't understand that the backticks at the start of a multi-line code block need to be on a separate line; in which case this should have been dupe-hammered to some code formatting reference. Commented Apr 5 at 8:17

2 Answers 2

3

Even though OP says it’s unreadable, the table shown in the question looks good enough. However, here are some suggestions to improve readability (remember, the goal to include a minimal, reproducible example, not the entire dataset).

  1. This many columns are never needed, so it’s probably better to cut down the number of columns.
  2. Unless the question is about the specific values in the columns, it’s probably better to use a generic strings, numbers etc. instead of say, 20809_451291030_17944026 or ainda nao recebi minha senha.

With that being said, instead of worrying about how to show a table in a nice format on Stack Overflow, questions should include the sample data to construct the table. Answerers need to test their answers and it is far more useful for them (and anyone else coming to the Q/A) if the data is given instead of them trying to parse the data from a table.

Besides, I would wager that 99.99% of users who answer questions in the tag use an IDE locally that would easily let them visualize the data in table format.

For the example in the OP, the constructor data would look like the following:

df = pd.DataFrame({
    'id': ['20809_451291030_17944026', '20810_459751030_18018215', '20810_455341030_17968671', '20810_455341030_17968671', '20809_458715030_18010104'], 
    'text': ['ainda nao recebi minha senha', 'ainda nao recebi minha senha', 'ainda nao recebi a senha', 'ainda nao recebi a senha', 'ainda nao recebi a senha'], 
    'top_class': [1001, 1001, 1001, 1001, 1001], 
    'class_name': [1001, 1001, 1001, 14008, 1001], 
    'confidence': [0.976569, 0.976569, 0.97145, 0.011035, 0.97145]
})
1
  • 1
    Hard agree. The only thing I would add is that it's often useful to visualize the data in table format without having to load it, so in addition to the constructor data, I would include the output from print(df) or print(df.to_markdown()), whichever looks better.
    – wjandrea
    Commented Apr 5 at 21:02
-2

You can include the dataframe as a markdown table:

print(df.to_markdown())

And then answerers can ingest it directly with pandas:

df = pd.read_html('https://meta.stackoverflow.com/q/391900')[0]
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .