How to print dataframe in python?

Question:

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:

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
Asked By: LOG

||

Answers:

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
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.