Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There appears to be a bug in the current version of Pandas ('0.11.0'), which means that Matti John's answer will not work. If you specify columns for writing to file, they are written in alphabetical order, but simply relabelled according to the list in cols. For example, this code:</p> <pre><code>import pandas dfdict={} dfdict["a"]=[1,2,3,4] dfdict["b"]=[5,6,7,8] dfdict["c"]=[9,10,11,12] df=pandas.DataFrame(dfdict) df.to_csv("dfTest.txt","\t",header=True,cols=["b","a","c"]) </code></pre> <p>results in this (incorrect) output:</p> <pre><code> b a c 0 1 5 9 1 2 6 10 2 3 7 11 3 4 8 12 </code></pre> <p>You can check which version of pandas you have installed by executing:</p> <pre><code>pandas.version.version </code></pre> <p>Documentation for to_csv is <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html" rel="noreferrer">here</a></p> <p>Actually, it seems that this is a known bug and will be fixed in an upcoming release (0.11.1):</p> <p><a href="https://github.com/pydata/pandas/issues/3489" rel="noreferrer">https://github.com/pydata/pandas/issues/3489</a></p> <p>UPDATE: There still hasn't been a new release of pandas, but there is a workaround described here, which doesn't require using a different version of pandas: </p> <p><a href="https://github.com/pydata/pandas/issues/3454" rel="noreferrer">github.com/pydata/pandas/issues/3454</a> </p> <p>So changing the last line in the block of code above to the following will work correctly: </p> <pre><code>df.to_csv("dfTest.txt","\t",header=True,cols=["b","a","c"], engine='python') </code></pre> <p>UPDATE it seems that the argument "cols" has been renamed to "columns" and that the argument "engine" is deprecated (no longer available) in recent versions of pandas. Also, this bug is fixed in version 0.19.0.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload