2

I frequently find myself writing code like this to get nicely formatted multi-column output (without the index) when debugging or studying my data in pandas:

   dfs = dfs[dfs['some_id'] == the_id]
    cols = [
      'some_col',
      'another_col',
      'yet_another',
    ]

    print("\t".join(cols))
    for row in dfs[cols].values:
      print("\t\t".join([str(val) for val in row]))

This works fine, but I was wondering if there's a built in way to get this sort of output with a pandas function or direct lookup syntax. Sample output:

some_col    another_col    yet_another
a           b              c
x           y              z

1 Answer 1

2

Yes, you can call df.to_string with the parameter index=False.

dfs = dfs[dfs['some_id'] == the_id]
    cols = [
      'some_col',
      'another_col',
      'yet_another',
    ]

print(dfs[cols].to_string(index=False))

MCVE:

print(df)

          0         1
0  0.335232 -1.256177
1 -1.367855  0.746646
2  0.027753 -1.176076
3  0.230930 -0.679613
4  1.261967  0.570967

print(df.to_string(index=False, col_space=10))

0          1
 0.335232  -1.256177
-1.367855   0.746646
 0.027753  -1.176076
 0.230930  -0.679613
 1.261967   0.570967
Sign up to request clarification or add additional context in comments.

2 Comments

I usually use that to see all or a single column. How can I use that to select a subset of the total columns?
@JB Added the edit for your particular code at the top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.