0

My code runs through XML files and parses certain data. I'm using pandas to organize, dataframe, and write to CSV which looks like:

                    data = {"Name":inv_names, "Publication Date":date_pub, "Document Number":doc_num, "Patcit":name_citation, "Npatcit":data2, "Country":data3, "MainClass":data13}

                    df = pd.concat([df, pd.DataFrame(data)])

                    with open("./output.csv", "a", encoding = 'utf-8') as f:

                        df.to_csv(f, index = False)

This prints to the CSV file like:

Country Document Number MainClass   Name    Npatcit Patcit  Publication Date

All I really want to do is re-order this data like:

Name   Publication Date  Document Number ..... (just like the order given by data above)

Might be a simple case but I've tried things like df.sort_values() by different types but nothing seems to work.

HelloToEarth
  • 2,027
  • 3
  • 22
  • 48
  • Another suggestion is to not use a regular dict object because the order of the key:value pairs are not preserved. You can use OrderedDict (from collections import OrderedDict) which will keep the key:value order. – larrywgray Dec 19 '17 at 04:30

1 Answers1

1

You can reorder the columns manually

df = df[['Name','Publication Date','Document Number'....]]
Alex Zisman
  • 411
  • 2
  • 9