Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在大量列中更改数据帧中特定列的位置(索引)?_Python_Dataframe_Indexing_Multiple Columns - Fatal编程技术网

Python 在大量列中更改数据帧中特定列的位置(索引)?

Python 在大量列中更改数据帧中特定列的位置(索引)?,python,dataframe,indexing,multiple-columns,Python,Dataframe,Indexing,Multiple Columns,我有一个大约800列的数据框,如下所示 my_data: c1, c2, c3, ...,c340,...,c800 0 1, 0, 1, ..., 0,... , 1 1 1, 1, 0, ..., 1,... , 1 .. .. .. 因此,我想将例如列“c340”移动到数据帧的末尾,就像上面示例中c800的位置一样。因此,预期结果应如下所示: my_data: c1, c2, c3, ...,...,c800,c340 0 1

我有一个大约800列的数据框,如下所示

 my_data:
    c1, c2, c3, ...,c340,...,c800
 0   1,  0,  1, ..., 0,...  ,  1
 1   1,  1,  0, ..., 1,...  ,  1
 ..  ..  ..
因此,我想将例如列“c340”移动到数据帧的末尾,就像上面示例中c800的位置一样。因此,预期结果应如下所示:

  my_data:
    c1, c2, c3, ...,...,c800,c340
 0   1,  0,  1, ...,...  ,  1, 0
 1   1,  1,  0, ...,...  ,  1, 1
我可以使用下面的代码对列进行排序,但是,由于列的数量很多,手动更改不是一个好主意

 my_data = my_data[['c1','c2',... 'c340']]

我所能想到的就是解决你的问题

df_columns = my_data.columns.tolist()

df_columns.append(df_columns.pop(df_columns.index('c340')))  # ['c1','c2',...,'c800','c340']

my_data = my_data[df_columns]

显示预期的格式以及您尝试了什么