Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing index from pandas data frame on print

I’m really struggling to get this to print the way I want to. I’ve read through the documentation on removing index, but it seems like it still shows up. Here is my code:

quotes = pd.read_csv("quotes.txt",header = None, index_col = False)
quote_to_send = quotes.sample(ignore_index = True)
print(quote_to_send)

The text file isn’t anything special, looks like this:

"When you arise in the morning think of what a privilege it is to be alive, to think, to enjoy, to love..."  - Marcus Aurelius

"Either you run the day or the day runs you." - Jim Rohn
....

The output of this looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                                               0
0  You may have to fight a battle more than once ...

How do I get rid of those random 0s?

>Solution :

The 0 on top is your column name, since you don’t have one…

The 0 on the left is your index, something that absolutely every dataframe needs.

If you really want to see things without those essential pieces, you can use

print(df.to_string(header=False, index=False))

When you arise in the morning think of what a privilege it is to be alive, to think, to enjoy, to love...  - Marcus Aurelius
                                                                      Either you run the day or the day runs you. - Jim Rohn
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading