Rebin Rebin - 7 months ago 32
Python Question

Python Pandas to_csv method formatting

I create a 2D matrix using PANDAS with the following code:

def constructTransition(originalList):

st = np.ones((26, 26))
s1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z']
stateProbability = pd.DataFrame(st, index=s1, columns=s2)

# FILLING the matrix


stateProbability.to_csv("StateProbability.csv", sep='\t', float_format='%.12f')


This is working fine except that it prints like this:

a b c d e f g h i j k l m n o p q r s t u v w x y z
a 0.000138370001 0.009685900097 0.100733361007 0.013421890134 0.088695170887 0.018403210184 0.028227480282 0.179465891795 0.051335270513 0.000553480006 0.000276740003 0.049121350491 0.111111111111 0.042617960426 0.011484710115 0.031686730317 0.000138370001 0.087449840874 0.025736820257 0.063096720631 0.021308980213 0.019371800194 0.028227480282 0.007887090079 0.001522070015 0.008302200083
b 0.354294478528 0.007668711656 0.001533742331 0.001533742331 0.035276073620 0.001533742331 0.001533742331 0.013803680982 0.116564417178 0.001533742331 0.001533742331 0.001533742331 0.070552147239 0.001533742331 0.250000000000 0.001533742331 0.001533742331 0.019938650307 0.004601226994 0.003067484663 0.087423312883 0.001533742331 0.001533742331 0.001533742331 0.015337423313 0.001533742331
c 0.116357504216 0.000562113547 0.021922428331 0.000843170320 0.209949409781 0.001686340641 0.000281056773 0.000281056773 0.229342327150 0.000281056773 0.000281056773 0.001967397414 0.000281056773 0.116638560989 0.137436762226 0.000281056773 0.000281056773 0.021641371557 0.044406970208 0.009836987071 0.061270376616 0.000281056773 0.000281056773 0.005340078696 0.017987633502 0.000281056773
d 0.080699774266 0.000282167043 0.000282167043 0.007054176072 0.274830699774 0.000282167043 0.000282167043 0.000282167043 0.085214446953 0.000282167043 0.005079006772 0.094525959368 0.000282167043 0.327313769752 0.069413092551 0.000282167043 0.000282167043 0.033013544018 0.000282167043 0.000564334086 0.015237020316 0.000282167043 0.003103837472 0.000282167043 0.000282167043 0.000282167043
e 0.000121824938 0.049582749589 0.040263141865 0.047146250838 0.028811597734 0.015776329415 0.023329475544 0.164768228056 0.041055003959 0.001522811720 0.013339830663 0.063653529878 0.041055003959 0.050435524152 0.003837485533 0.034110982518 0.000060912469 0.120972163002 0.062800755315 0.082901870013 0.009563257599 0.067491015411 0.028628860328 0.001827374063 0.002862886033 0.004081135408
...
...


As you can see the letters correspond to the columns are not tab delimited and thy are not in the center of their corresponding column.
How can I force the "HEADERS" to be in the center of the columns?

Answer

you can use:

pd.set_option('display.expand_frame_repr', False)
pd.set_option('display.max_columns', 99)
pd.set_option('display.float_format', '%.12f')

then just print your DF:

print(df)

n [34]: stateProbability
ut[34]:
              a              b              c              d              e              f              g              h              i
    j              k              l              m              n              o              p              q              r              s
         t              u              v              w              x              y              z
 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456
89012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.1
3456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012
 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456
89012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.1
3456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012
 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456
89012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.1
3456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012
 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456
89012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.1
3456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012 0.123456789012