Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving column order in the pandas to_csv method

The to_csv method of pandas does not preserve the order of columns. It chooses to alphabetically arrange the columns in CSV. This is a bug and has been reported and is supposed to be corrected in version 0.11.0. I have 0.18.0.

import pandas as pd
df = pd.DataFrame({'V_pod_error' : [a],
                   'V_pod_used' : [b],
                   'U_sol_type' : [c]
                                ...
                                ... and so on upto 50 columns }

pd.to_csv(df)

Excel order:

0   U_sol type          V_pod_error      V_pod_used      ...
1

What I want is order in the dictionary:

0   V_pod_error      V_pod_used          U_sol type     ...
1

I have a huge number of columns and names. I cannot do it manually or write out the column order. There has been the exact same question in 2013 here. And it doesnt look like there is an update! I would like to ask the community to help me out! This is really problematic.

like image 510
agent18 Avatar asked May 06 '16 11:05

agent18


People also ask

Does pandas preserve column order?

Pandas. DataFrame doesn't preserve the column order when converting from a DataFrames.

What does To_csv do in pandas?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.

Does pandas To_csv overwrite?

When you write pandas DataFrame to an existing CSV file, it overwrites the file with the new contents. To append a DataFrame to an existing CSV file, you need to specify the append write mode using mode='a' .

How do I change the order of columns in panda?

You can change the order of columns in the pandas dataframe using the df. reindex() method.


Video Answer


1 Answers

Try the following solution. Even I faced the same issue. I solved it as follows:

import pandas as pd
df = pd.DataFrame({'V_pod_error' : [a],
                   'V_pod_used' : [b],
                   'U_sol_type' : [c]
                                ...
                                ... and so on upto 50 columns }

column_order = ['V_pod_error', 'V_pod_used', 'U_sol_type',.....# upto 50 column names]

df[column_order].to_csv(file_name)
like image 161
Saranya Krishnamurthy Avatar answered Sep 19 '22 02:09

Saranya Krishnamurthy