Indeed I see the problem and where the "bug" is caused. The reason the
extra space is there is to make space for the '-' sign in numerical
columns. In the default console formatting this is (at least in my
opinion) the most attractive choice. When you pass custom formatters
it should not do this so you have very explicit control over how
things look. Basically the default formatter for floats is something
like:
if x < 0:
return '%.4f' % x
else:
return ' %.4f' % x
the result is something that looks like
B
4
-1.5
5.6
-6.7
instead of the much uglier
B
4
-1.5
5.6
6.7
In order to get the column label to line up on the first digit you
have to add a space.
One solution is to right-justify the column name (it's currently
left-justified). I'll see what I can do-- GitHub issue for this would
be useful for me so I don't forget to look into it.
- Wes
Adam and I (mostly Adam) hacked on this this week.
To Texas P's question about the extra space and formatting: to get a
pretty output you need to right-justify the column names. Currently
pandas left-justifies by default-- I'm considering making
right-justify the default behavior, or perhaps configurable as an
option in set_printoptions (just for the column names for now), it
seems to be fairly common elsewhere (e.g. R):
DF=DataFrame({'i':[1,1,1],'j':[1,2,3],'k':[4,4,4],'mass_g_d':
[1.1,2.5,10.4]})
intformatter = lambda x: '%10i' %x
floatformatter = lambda x: '%10.5f' %x
print DF.to_string(columns=['k','i','j','mass_g_d'],
index_names=False, justify='right',
col_space=0,
formatters={'k':intformatter,
'i':intformatter,
'j':intformatter,
'mass_g_d':floatformatter})
## -- End pasted text --
k i j mass_g_d
0 4 1 1 1.10000
1 4 1 2 2.50000
2 4 1 3 10.40000
Any extra thoughts on the matter?
- Wes