Python Pandas to_csv方法格式

2024-03-28 18:20:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我用PANDAS创建了一个二维矩阵,代码如下:

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

除了打印如下:

^{pr2}$

如您所见,与列相对应的字母没有制表符分隔,而您的字母也不在相应列的中心。 如何强制“标题”位于列的中心?在


Tags: csv代码pandasdefnp字母ones矩阵