在 Pandas to_csv 方法中保留列顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37070759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:11:29  来源:igfitidea点击:

Preserving column order in the pandas to_csv method

pythonexcelcsvpandas

提问by Thej Kiran

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.

pandas 的 to_csv 方法不保留列的顺序。它选择按字母顺序排列 CSV 中的列。这是一个错误,已被报告,应该在 0.11.0 版中更正。我有 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:

Excel订单:

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.

我有大量的列和名称。我无法手动完成或写出列顺序。2013 年这里有完全相同的问题。而且看起来好像没有更新!我想请社区帮助我!这确实有问题。

采纳答案by Saranya Krishnamurthy

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)

回答by jezrael

I think problem is in DataFrameconstructor, because you need add parameter columnsfor custom ordering of columns. If you dont set parameter columns, columns are ordered alphanumerical.

我认为问题出在DataFrame构造函数中,因为您需要columns为列的自定义排序添加参数。如果不设置参数列,则列按字母数字顺序排列。

import pandas as pd
df = pd.DataFrame({'V_pod_error' : [0,2],
                   'V_pod_used' : [6,4],
                   'U_sol_type' : [7,8]})
print df
   U_sol_type  V_pod_error  V_pod_used
0           7            0           6
1           8            2           4

print df.to_csv()
,U_sol_type,V_pod_error,V_pod_used
0,7,0,6
1,8,2,4


df1 = pd.DataFrame({'V_pod_error' : [0,2],
                   'V_pod_used' : [6,4],
                   'U_sol_type' : [7,8]}, 
                    columns=['V_pod_error','V_pod_used','U_sol_type'])

print df1
   V_pod_error  V_pod_used  U_sol_type
0            0           6           7
1            2           4           8

print df1.to_csv()
,V_pod_error,V_pod_used,U_sol_type
0,0,6,7
1,2,4,8

EDIT:

编辑:

Another solution is set order of column by subset before write to_csv(thanks Mathias711):

另一种解决方案是在写入之前按子集设置列顺序to_csv(感谢Mathias711):

import pandas as pd
df = pd.DataFrame({'V_pod_error' : [0,2],
                   'V_pod_used' : [6,4],
                   'U_sol_type' : [7,8]})
print df
   U_sol_type  V_pod_error  V_pod_used
0           7            0           6
1           8            2           4

df = df[['V_pod_error','V_pod_used','U_sol_type']]
print df

   V_pod_error  V_pod_used  U_sol_type
0            0           6           7
1            2           4           8

EDIT1: Maybe help first convert dictto OrderedDictand then create DataFrame:

EDIT1:也许有助于首先转换dictOrderedDict然后创建DataFrame

import collections
import pandas as pd


d = {'V_pod_error' : [0,2],'V_pod_used' : [6,4], 'U_sol_type' : [7,8]}
print d
{'V_pod_error': [0, 2], 'V_pod_used': [6, 4], 'U_sol_type': [7, 8]}

print pd.DataFrame(d)
   U_sol_type  V_pod_error  V_pod_used
0           7            0           6
1           8            2           4

d1 = collections.OrderedDict(d)
print d1
OrderedDict([('V_pod_error', [0, 2]), ('V_pod_used', [6, 4]), ('U_sol_type', [7, 8])])

print pd.DataFrame(d1)
   V_pod_error  V_pod_used  U_sol_type
0            0           6           7
1            2           4           8

回答by Biranchi

Try with:

尝试:

df.to_csv(file_name, sep=',', encoding='utf-8', header=True, columns=["Col1","Col2","Col3","Col4"])

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html