Insert row and col label when dumping matrix data to text file in python
<p>I want to insert labels to col and row. like below.</p>

<pre><code>    x   y   z
a   1   3   4
b   4   3   2
</code></pre>

<p>Actually I could do to add header like below.</p>

<pre><code>import numpy as np
row_label = ["a", "b"]
col_label = ["x", "y", "z"]
data = np.array([[1,3,4], [4,3,2]])
np.savetxt("out.csv", data, header=",".join(["x", "y", "z"]), delimiter=",", fmt="%.0f", comments='')
</code></pre>

<p>out.csv</p>

<pre><code>x,y,z
1,3,4
4,3,2
</code></pre>

<p>But how do I also add a column label?</p>

<pre><code>import numpy as np
import pandas as pd
row_label = ["a", "b"]
col_label = ["x", "y", "z"]
data = np.array([[1,3,4], [4,3,2]])
df = pd.DataFrame(data, index=row_label, columns=col_label)
df.to_csv(r'temp.csv', sep='\t')
</code></pre>

<p>temp.csv</p>

<pre><code>    x   y   z
a   1   3   4
b   4   3   2
</code></pre>

python numpy