0

issue1

Q: How to remove index and format output of table without leading whitespace for a dataframe object in pandas in Python?

Code:

import pandas as pd

df2 = pd.DataFrame({'Record Type': ['100', '200', '300'],'Value': [(1,2,3,4,5), (0,10), 1]},index = (['','','']))

print(df2)

I run it in Anaconda Spyder console.

And I expect it displays.

Record Type            Value
        100  (1, 2, 3, 4, 5)
        200          (0, 10)
        300                1

However, it displays with a leading whitespace.

 Record Type            Value
     100  (1, 2, 3, 4, 5)
     200          (0, 10)
     300                1

Any replies will be helpful.

I post it in stackoverflow and Github.

stackoverflow:

Github:

https://github.com/40843245/Pandas/blob/main/Tutorial/Issues/issue%231.md

Although I have google it and find API of pandas, I just find a way to achieve it -- set the index as empty string ''.

3
  • print(df2.to_string(index=False)) or in jupyter from IPython.display import HTML ; HTML(df.to_html(index=False)) Commented Sep 15, 2023 at 4:46
  • Thanks to @mozway. It works for me to use df2.to_string(index=False). However, It returns a str object. I wanna get a dataframe object. How to achieve that? For more details, see the post Github as shown link inthe question. Commented Sep 16, 2023 at 1:34
  • Then use the html variant Commented Sep 16, 2023 at 5:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.