1

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.

cwind
  • 369
  • 1
  • 7
  • Possible duplicate of [Using formatters to make string from dataframe](https://stackoverflow.com/questions/44793699/using-formatters-to-make-string-from-dataframe) – JohanL Jul 07 '17 at 06:50
  • Haha, mine question seems partialy resolve the possible duplicate problem by not perfectly. So the focus is something different? (I donnot know how to deal) , I have update the question. – cwind Jul 07 '17 at 09:42
  • No, your issue is exactly the same. Leading spaces of the first element of the first row are ignored when `header=False`. – JohanL Jul 07 '17 at 12:44
  • Okay, I have not catch the meaning of the previous posts of Hatshepsut . After deep reading, you are right. But the answer is not perfect in Hatshepsut. So I'd like to keep this post? or According to rules of stackoverflow, I must delete this posts? – cwind Jul 07 '17 at 14:20
  • No, but someone with a higher reputation than me, might decide to turn this into a duplicate. You can just leave it as it is. – JohanL Jul 07 '17 at 18:29
  • ok, thanks for your advice – cwind Jul 09 '17 at 12:10

0 Answers0