Python Pandas to_csv方法格式化

时间:2016-05-04 18:10:26

标签: python csv pandas dataframe export-to-csv

我使用PANDAS使用以下代码创建2D矩阵:

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')

这样工作正常,除了它打印如下:

    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
...
...

正如您所看到的,对应于列的字母不是制表符分隔的,并且您不在其对应列的中心。 如何强制“HEADERS”位于列的中心?

1 个答案:

答案 0 :(得分:1)

你可以使用:

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

然后打印你的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