I'm looping through a JSON response and appending each element to a Pandas DataFrame in Python.
At the end of the loop, I output the DataFrame to a CSV. However, each time I output the CSV, the headers aren't consistent, as in the order of the columns seem to change.
How can I make the order of the headers (and columns) consistent each time? See the code that I'm working with below:
output = pd.DataFrame()
for item in json_resp:
json_struct = {
'col_1':json_resp['data'],
'col_2':json_resp['data_2'],
'col_2':json_resp['data_2']
}
output = output.append(json_struct, ignore_index=True)
output.to_csv('csv_output.csv', index=False, encoding='utf-8-sig')