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

How to print dataframe in python?

Given the following data frame:

          Name   Telephone  Telephone2
0   Bill Gates  555 77 854  555 77 855
1  Bill Gates2           2           3

How can I have exactly the following output(printing column by column)?

Name
Bill Gates
Bill Gates2
Telephone
555 77 854
2
Telephone2
555 77 855
3

I tried:

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

for key, val in df.iterrows():
    for sub_val in val:
        print(sub_val)

But I get:

Bill Gates
555 77 854
555 77 855
Bill Gates2
2
3

>Solution :

You can use:

for col in df:
    print(col)
    print('\n'.join(df[col]))

Output:

Name
Bill Gates
Bill Gates2
Telephone
555 77 854
2
Telephone2
555 77 855
3
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