0

For example, say I have computed two variables A and B, both numpy arrays.

Now I would like to make a dataframe out of them, then write to CSV, so that B goes before A. However, when I try:

out_df = pd.DataFrame({'B':B,'A':A})
out_df.head()

I see that column A has been placed to the left of column B. In some analyses, this won't matter, but I specifically want column B first, then column A in the output csv.

How do I tell pd.DataFrame to respect the ordering of the columns as I specify them?

Laurent
  • 12,287
  • 7
  • 21
  • 37
yurnero
  • 315
  • 2
  • 9
  • 1
    `out_df[['B', 'A']].to_csv(...)` – Psidom Jun 25 '21 at 17:29
  • @Psidom That would work, but is it possible to make `out_df` "right" in the place, so that I don't need to do something like you suggest at the `to_csv` step? – yurnero Jun 25 '21 at 17:30
  • 2
    You pass the data as dictionary, which does not have order of keys. After you create a dataframe, do the `df=df[["B", "A"]]`. – crayxt Jun 25 '21 at 17:34
  • pandas version 1.2.1 and pandas version 1.2.4 cannot replicate the issue: out_df has columns in order of keys in the dictionary. (Python 3.8.5) – dm2 Jun 25 '21 at 17:34
  • Given the current data you have, this is the easiest way to reorder columns. But sure you can do `pd.DataFrame(list(zip(B, A)), columns=['B', 'A'])` – Psidom Jun 25 '21 at 17:35
  • 2
    What is your python version? IIRC, python once didnot retain order of a dict( and we used to use OrderedDict). Upgrade your version maybe, EDIT: okay got it its 3.6+ https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6 – anky Jun 25 '21 at 17:37
  • @anky I have Python 3.6.0 and Pandas 0.20.2. It's a work setup and I may not be able to change these versions. – yurnero Jun 25 '21 at 17:42

0 Answers0