title: Export pandas dataframe to json and back to a dataframe with columns in the same order
body: <p>I wrote two small programs: </p>
  
  <ul>
  <li>The first one imports data from a .txt file into a pandas data frame, manipulates the data and exports the final data to a json file. </li>
  <li>The second code imports the data from that json file back into a data frame.</li>
  </ul>
  
  <p>Unfortunately, the order of the columns changes when the data is imported from the json file. I have seen several examples online, where <code>OrderedDict</code> was used to create a fixed structure in a new table, but how can I apply <code>OrderedDict</code> to an existing table? </p>
  
  <p>I have tried several versions including the ones below, but none of them worked:</p>
  
  <pre><code>df = OrderedDict(pd.DataFrame.from_dict(json_data, orient='columns'))
  </code></pre>
  
  <p>and</p>
  
  <pre><code>data = OrderedDict(pd.read_csv('wtx2015.txt', sep=",", header=None))
  </code></pre>
  
  <p><strong>Code: .txt > pandas data frame > json</strong>    </p>
  
  <pre><code>import pandas as pd
  import json
  from pandas import DataFrame
  from collections import OrderedDict
  
  pd.set_option("max_columns", 50)
  
  """Defining functions"""
  
  data = pd.read_csv('wtx2015.txt', sep=",", header=None)
  data.columns = ["category1", "category2", "category3", "category4"]
  
  """Manipulating data"""
  
  print(data.head(n=3))
  
  df = DataFrame(data, columns= ["category1", "category2", "category3", "category4", "category5"])
  
  final = df.to_json(orient='records')
  with open('pandas_test.json', 'w') as f_obj:
      f_obj.write(final)
  </code></pre>
  
  <p><strong>Code: json > pandas data frame</strong></p>
  
  <pre><code>import pandas as pd
  import json
  
  file = 'pandas_test.json'
  with open(file) as f_obj:
      json_data = json.load(f_obj)
  
  df = pd.DataFrame.from_dict(json_data, orient='columns')
  
  print(df)
  </code></pre>