specify number of spaces between pandas DataFrame columns when printing

HaroldFinch

When you print a pandas DataFrame, which calls DataFrame.to_string, it normally inserts a minimum of 2 spaces between the columns. For example, this code

import pandas as pd

df = pd.DataFrame( {
    "c1" : ("a", "bb", "ccc", "dddd", "eeeeee"),
    "c2" : (11, 22, 33, 44, 55),
    "a3235235235": [1, 2, 3, 4, 5]
} )
print(df)

outputs

       c1  c2  a3235235235
0       a  11            1
1      bb  22            2
2     ccc  33            3
3    dddd  44            4
4  eeeeee  55            5

which has a minimum of 2 spaces between each column.

I am copying DataFarames printed on the console and pasting it into documents, and I have received feedback that it is hard to read: people would like more spaces between the columns.

Is there a standard way to do that?

I see no option in either DataFrame.to_string or pandas.set_option.

I have done a web search, and not found an answer. This question asks how to remove those 2 spaces, while this question asks why sometimes only 1 space is between columns instead of 2 (I also have seen this bug, hope someone answers that question).

My hack solution is to define a function that converts a DataFrame's columns to type str, and then prepends each element with a string of the specified number of spaces.

This code (added to the code above)

def prependSpacesToColumns(df: pd.DataFrame, n: int = 3):
    spaces = ' ' * n
    
    # ensure every column name has the leading spaces:
    if isinstance(df.columns, pd.MultiIndex):
        for i in range(df.columns.nlevels):
            levelNew = [spaces + str(s) for s in df.columns.levels[i]]
            df.columns.set_levels(levelNew, level = i, inplace = True)
    else:
        df.columns = spaces + df.columns
    
    # ensure every element has the leading spaces:
    df = df.astype(str)
    df = spaces + df
    
    return df

dfSp = prependSpacesToColumns(df, 3)
print(dfSp)

outputs

          c1     c2    a3235235235
0          a     11              1
1         bb     22              2
2        ccc     33              3
3       dddd     44              4
4     eeeeee     55              5

which is the desired effect.

But I think that pandas surely must have some builtin simple standard way to do this. Did I miss how?

Also, the solution needs to handle a DataFrame whose columns are a MultiIndex. To continue the code example, consider this modification:

idx = (("Outer", "Inner1"), ("Outer", "Inner2"), ("Outer", "a3235235235"))
df.columns = pd.MultiIndex.from_tuples(idx)
ALollz

You can accomplish this through formatters; it takes a bit of code to create the dictionary {'col_name': format_string}. Find the max character length in each column or the length of the column header, whichever is greater, add some padding, and then pass a formatting string.

Use partial from functools as the formatters expect a one parameter function, yet we need to specify a different width for each column.

Sample Data

import pandas as pd
df = pd.DataFrame({"c1": ("a", "bb", "ccc", "dddd", 'eeeeee'),
                   "c2": (1, 22, 33, 44, 55),
                   "a3235235235": [1,2,3,4,5]})

Code

from functools import partial

# Formatting string 
def get_fmt_str(x, fill):
    return '{message: >{fill}}'.format(message=x, fill=fill)

# Max character length per column
s = df.astype(str).agg(lambda x: x.str.len()).max() 

pad = 6  # How many spaces between 
fmts = {}
for idx, c_len in s.iteritems():
    fill = max(len(str(idx)), c_len) + pad - 1
    fmts[idx] = partial(get_fmt_str, fill=fill)

print(df.to_string(formatters=fmts))

            c1      c2      a3235235235
0            a       1                1
1           bb      22                2
2          ccc      33                3
3         dddd      44                4
4       eeeeee      55                5

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

specify number of spaces between pandas DataFrame columns when printing

From Dev

How to add underline separator between rows when printing Pandas dataframe?

From Dev

Reduce number of columns in a pandas DataFrame

From Dev

pandas dataframe computation between columns

From Dev

how to split 'number' to separate columns in pandas DataFrame

From Dev

Number of levels (depth) of index and columns in a Pandas DataFrame

From Dev

pandas dataframe drop columns by number of nan

From Dev

Restructuring pandas dataframe based on number of columns

From Dev

Pandas Dataframe Transpose to varying number of columns

From Dev

Dropping a number of columns in a pandas DataFrame on one line

From Dev

matching of columns between two pandas dataframe

From Dev

pandas DataFrame - find max between offset columns

From Dev

Fuzzy Matching between 2 columns in pandas dataframe

From Dev

Renaming columns when querying with SQLAlchemy into Pandas DataFrame

From Dev

Avoid sorting columns when unstacking a pandas dataframe

From Dev

Printing number of columns in for loop in R

From Dev

TypeError from merge pandas DataFrame on columns when item in columns is list

From Dev

Even spaces between bootstrap columns

From Dev

Using a list to specify pandas columns

From Dev

Using a list to specify pandas columns

From Dev

Transform pandas dataframe columns to list according to number in row

From Dev

How to count the number of persons with values present in n columns of a pandas dataframe

From Dev

add a row in pandas dataframe without knowing number of columns

From Dev

Remove spaces between a word and a number

From Java

Calculate Pandas DataFrame Time Difference Between Two Columns in Hours and Minutes

From Dev

Python: how to add a column to a pandas dataframe between two columns?

From Dev

Grouped function between 2 columns in a pandas.DataFrame?

From Dev

Vectorized/Matrix calculation between 2 Pandas dataframe columns

From Dev

Only allow one to one mapping between two columns in pandas dataframe

Related Related

  1. 1

    specify number of spaces between pandas DataFrame columns when printing

  2. 2

    How to add underline separator between rows when printing Pandas dataframe?

  3. 3

    Reduce number of columns in a pandas DataFrame

  4. 4

    pandas dataframe computation between columns

  5. 5

    how to split 'number' to separate columns in pandas DataFrame

  6. 6

    Number of levels (depth) of index and columns in a Pandas DataFrame

  7. 7

    pandas dataframe drop columns by number of nan

  8. 8

    Restructuring pandas dataframe based on number of columns

  9. 9

    Pandas Dataframe Transpose to varying number of columns

  10. 10

    Dropping a number of columns in a pandas DataFrame on one line

  11. 11

    matching of columns between two pandas dataframe

  12. 12

    pandas DataFrame - find max between offset columns

  13. 13

    Fuzzy Matching between 2 columns in pandas dataframe

  14. 14

    Renaming columns when querying with SQLAlchemy into Pandas DataFrame

  15. 15

    Avoid sorting columns when unstacking a pandas dataframe

  16. 16

    Printing number of columns in for loop in R

  17. 17

    TypeError from merge pandas DataFrame on columns when item in columns is list

  18. 18

    Even spaces between bootstrap columns

  19. 19

    Using a list to specify pandas columns

  20. 20

    Using a list to specify pandas columns

  21. 21

    Transform pandas dataframe columns to list according to number in row

  22. 22

    How to count the number of persons with values present in n columns of a pandas dataframe

  23. 23

    add a row in pandas dataframe without knowing number of columns

  24. 24

    Remove spaces between a word and a number

  25. 25

    Calculate Pandas DataFrame Time Difference Between Two Columns in Hours and Minutes

  26. 26

    Python: how to add a column to a pandas dataframe between two columns?

  27. 27

    Grouped function between 2 columns in a pandas.DataFrame?

  28. 28

    Vectorized/Matrix calculation between 2 Pandas dataframe columns

  29. 29

    Only allow one to one mapping between two columns in pandas dataframe

HotTag

Archive