擅长:python、mysql、java
<p>这几乎是以下内容的完全复制:<br/>
<a href="https://stackoverflow.com/questions/31247198/python-pandas-write-content-of-dataframe-into-text-file">Python, Pandas : write content of DataFrame into text File</a></p>
<p>我在这里再次报告引用的SO问题的答案,并对其进行了一些非常小的修改以适合本案例。<br/>
你可以用两种方法。</p>
<p><code>np.savetxt()</code>,在这种情况下,您应该具有如下内容:</p>
<pre><code>np.savetxt('xgboost.txt', a.values, fmt='%d', delimiter="\t", header="X\tY\tZ\tValue")
</code></pre>
<p>假设<code>a</code>是数据帧。当然,您可以更改所需的分隔符(制表符、逗号、空格等)。<br/>
另一个选项,如我所附的答案和@MYGz的答案中所述,是使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html#pandas.DataFrame.to_csv" rel="noreferrer">to_csv</a>方法,即:</p>
<pre><code>a.to_csv('xgboost.txt', header=True, index=False, sep='\t', mode='a')
</code></pre>