将数据帧转换为csv时如何防止行的字母顺序排列?

时间:2019-06-17 12:04:58

标签: pandas csv dataframe

我正在尝试使用pandas(csv)的to_csv()函数将以下数据帧转换为v0.24.0文件:

             robustness  orseilline  disrupt
             jarkman     woodprint   antennariid
sublinguae   436         47588       60
coupee       994         67199       21
Sauropsida   494         97139       57
metapleural  652         85448       95
sulphuret    667         55142       96

行的顺序很重要,但是结果会自动按字母顺序对行进行排序:

,robustness,orseilline,disrupt
,jarkman,woodprint,antennariid
Sauropsida,436,47588,60
coupee,994,67199,21
metapleural,494,97139,57
sublinguae,652,85448,95
sulphuret,667,55142,96

DataFrame是使用一些可创建随机单元格的代码生成的:

# 'rows' is predefined by some other generating function
stubs = [random.sample(words.words(), self.n_rows) for x in range(self.n_stubs)]
stubs = pd.MultiIndex.from_arrays(stubs)
headers = [random.sample(words.words(), self.n_columns) for x in range(self.n_headers)]
headers = pd.MultiIndex.from_arrays(headers)
table = pd.DataFrame(data=rows, index=stubs, columns=headers)

当将DataFrame转换为csv时,MultiIndex显然具有一些顺序(称为“代码”)。使用.to_latex()to_html()时,顺序被保留。该如何处理?

0 个答案:

没有答案