我们可以
ix通过传递列表来重新排序:
In [27]:# get a list of columnscols = list(df)# move the column to head of list using index, pop and insertcols.insert(0, cols.pop(cols.index('Mid')))colsOut[27]:['Mid', 'Net', 'Upper', 'Lower', 'Zsore']In [28]:# use ix to reorderdf = df.ix[:, cols]dfOut[28]:Mid Net Upper Lower ZsoreAnswer_option More_than_once_a_day 2 0% 0.22% -0.12% 65Once_a_day 3 0% 0.32% -0.19% 45Several_times_a_week 4 2% 2.45% 1.10% 78Once_a_week 6 1% 1.63% -0.40% 65另一种方法是引用该列,然后将其重新插入前面:
In [39]:mid = df['Mid']df.drop(labels=['Mid'], axis=1,inplace = True)df.insert(0, 'Mid', mid)dfOut[39]:Mid Net Upper Lower ZsoreAnswer_option More_than_once_a_day 2 0% 0.22% -0.12% 65Once_a_day 3 0% 0.32% -0.19% 45Several_times_a_week 4 2% 2.45% 1.10% 78Once_a_week 6 1% 1.63% -0.40% 65
从以后开始,您还可以使用
loc以获得与
ix以后版本的熊猫不建议使用的相同的结果
0.20.0:
df = df.loc[:, cols]



