The code as following showed:
a=pd.DataFrame({'element':['C','H','H','H'], 'x':[-2.55,-2.19,-2.19,-3.62], 'y':[ 0.6328,-0.3759, 1.1372, 0.6328], 'z':[ 0.00, 0.00,-0.87, 0.00]})
a
element x y z
0 C -2.55 0.6328 0.00
1 H -2.19 -0.3759 0.00
2 H -2.19 1.1372 -0.87
3 H -3.62 0.6328 0.00
b=a.to_string(formatters={'element': ' {:20s}'.format,'x': ' {:.8f}'.format,'y': ' {:.8f}'.format, 'z': ' {:.8f}'.format}, header=False,index=False))
the output is:
print(b)
C -2.55000000 0.63280000 0.00000000
H -2.19000000 -0.37590000 0.00000000
H -2.19000000 1.13720000 -0.87000000
H -3.62000000 0.63280000 0.00000000
so why the first line has no a blank. I want to add a blank column in the first column like this:
print(' '+b)
C -2.55000000 0.63280000 0.00000000
H -2.19000000 -0.37590000 0.00000000
H -2.19000000 1.13720000 -0.87000000
H -3.62000000 0.63280000 0.00000000
or
fl=open('zzz')
fl.writelines(' ')
fl.write(b)
The coding is something strange.
PS: what's the meaning of colon and comma in fomatters string? I was learning from the link Format certain floating dataframe columns into percentage in pandas
Is any manuals teach how to write the fomatters string
Additon: My question partially resolve the question: Using formatters to make string from dataframe , mine question is more focus on why the first line omit the blank from the formatters.