title: specify number of spaces between pandas DataFrame columns when printing
body: <p>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</p>
  <pre><code>import pandas as pd
  
  df = pd.DataFrame( {
      &quot;c1&quot; : (&quot;a&quot;, &quot;bb&quot;, &quot;ccc&quot;, &quot;dddd&quot;, &quot;eeeeee&quot;),
      &quot;c2&quot; : (11, 22, 33, 44, 55),
      &quot;a3235235235&quot;: [1, 2, 3, 4, 5]
  } )
  print(df)
  </code></pre>
  <p>outputs</p>
  <pre><code>       c1  c2  a3235235235
  0       a  11            1
  1      bb  22            2
  2     ccc  33            3
  3    dddd  44            4
  4  eeeeee  55            5
  </code></pre>
  <p>which has a minimum of 2 spaces between each column.</p>
  <p>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.</p>
  <p><em>Is there a standard way to do that?</em></p>
  <p>I see no option in either <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_string.html" rel="nofollow noreferrer">DataFrame.to_string</a> or <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.set_option.html" rel="nofollow noreferrer">pandas.set_option</a>.</p>
  <p>I have done a web search, and not found an answer.  <a href="https://stackoverflow.com/questions/52030631/remove-the-automatic-two-spaces-between-columns-that-pandas-dataframe-to-string">This question</a> asks how to remove those 2 spaces, while <a href="https://stackoverflow.com/questions/65772452/using-to-string-with-formatters-removes-one-space-between-columns-of-a-pandas">this question</a> asks why sometimes only 1 space is between columns instead of 2 (I also have seen this bug, hope someone  answers that question).</p>
  <p>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.</p>
  <p>This code (added to the code above)</p>
  <pre><code>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)
  </code></pre>
  <p>outputs</p>
  <pre><code>          c1     c2    a3235235235
  0          a     11              1
  1         bb     22              2
  2        ccc     33              3
  3       dddd     44              4
  4     eeeeee     55              5
  </code></pre>
  <p>which is the desired effect.</p>
  <p>But I think that pandas surely must have some <em>builtin</em> simple standard way to do this.  Did I miss how?</p>
  <p>Also, the solution needs to handle a DataFrame whose columns are a MultiIndex.  To continue the code example, consider this modification:</p>
  <pre><code>idx = ((&quot;Outer&quot;, &quot;Inner1&quot;), (&quot;Outer&quot;, &quot;Inner2&quot;), (&quot;Outer&quot;, &quot;a3235235235&quot;))
  df.columns = pd.MultiIndex.from_tuples(idx)
  </code></pre>