I'm looking for a solution to remove/turn off the 2 spaces between columns that df.to_string
creates automatically.
Example:
from pandas import DataFrame
df = DataFrame()
df = df.append({'a':'12345', 'b': '12345'})
df.to_string(index=False, header=False)
'12345 1235'
For clarity, the result is: '12345..12345' where the dots represent actual spaces.
I already tried the pandas.set_option
and pandas.to_string
documentation.
EDIT: The above example is overly simplified. I am working with an existing df that has spaces all over the place and the output text files are consumed by another blackbox program that is based off char-widths for each line. I've already figured out how to reformat the columns with formatters and make sure my columns are not cutoff by pandas default so I am 90% there (minus these auto spaces).
FYI here are some good links on to_string()
formatting and data-truncation:
Appreciate the help!
You can use the pd.Series.str.cat
method, which accepts a sep
keyword argument. By default sep
is set to ''
so there is no separation between values. Here are the docs: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.cat.html
You can also use pd.Series.str.strip
to remove any leading or trailing whitespace from each value. Here are the docs: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.strip.html
Here's an example based on what you have:
df = pd.DataFrame({'a': ['12345'], 'b': ['12345']})
df.iloc[0].fillna('').str.strip().str.cat(sep=' ')
Note that fillna('')
is required if there are any empty values.