Issue
I used to format the dataframe and output a file by:
tta = pandas.DataFrame(
{'name': ['xx', 'y y', 'z'], 'alter': ['AA', 'BB', 'CCC']})
str1 = tta.to_string(formatters={'name': '{:05s}'.format,
'alter': '{:08s}'.format
}, header=False, index=False, justify='right', col_space=0)
However, the to_string
function adds an extra space to my output string like the str1
in the script:
'xx000 AA000000\ny y00 BB000000\nz0000 CCC00000'
It seems it is hard to remove the extra white space. (See: Remove the automatic two spaces between columns that Pandas DataFrame.to_string inserts)
Thus is there any elegant way to format the data frame based on the column and let me get results like this:
'xx000AA000000\ny y00BB000000\nz0000CCC00000'
Solution
I don't think there is a straightforward way to achieve this, the space separators seem hardcoded in pandas' source, I didn't find an obvious way to change them. A simple hack however might be to add a custom placeholder (e.g. -*-
) in your format
to easily be able to remove it together with the following space afterwards:
PLACEHOLDER = '-*-'
str1 = (tta.to_string(formatters={'name': ('{:05s}'+PLACEHOLDER).format,
'alter': '{:08s}'.format
},
header=False, index=False, justify='right', col_space=0)
.replace(f'{PLACEHOLDER} ', '')
)
Output:
'xx000AA000000\ny y00BB000000\nz0000CCC00000'
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.