Home > Net >  How to print dataframe in python?
How to print dataframe in python?

Time:08-11

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

CodePudding user response:

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
  • Related