Search code examples
pandasstringpaddingtostring

pandas to_string without padding?


How can I get the to_string function in pandas to stop padding each line with spaces?

print(pd.Series(['string', 'another string', 'one more']).to_string(index=False))

prints

         string
 another string
       one more

but I would like

string
another string
one more

Solution

  • Not the answer to "why pandas doing that" but this may do the work for you: print('\n'.join(pd.Series(['string', 'another string', 'one more'])))

    In documentation of to_string, pandas says "Render a string representation of the Series." As it says "representation", not "equivalent" or something, pandas doing it this way. I'm not sure but I hope it helps.