Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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 基于列标题中的word将多个文件中的列设为最后一列_Python_Pandas - Fatal编程技术网

Python 基于列标题中的word将多个文件中的列设为最后一列

Python 基于列标题中的word将多个文件中的列设为最后一列,python,pandas,Python,Pandas,我有一个包含多个文件的文件夹,所有文件都有一个名为something\u last的列标题。我想让任何列中都有_last,文件夹中每个数据帧的最后一列。考虑到_最后一列的索引取决于df,一个不包含关键字的解决方案对我来说不起作用 这就是我所拥有的: df_1 df_2 这就是我想要的: df_1 df_2 您可以使用: columns=df.columns.tolist() columns.append(columns.pop([df.columns.tolist().index(key) fo

我有一个包含多个文件的文件夹,所有文件都有一个名为something\u last的列标题。我想让任何列中都有_last,文件夹中每个数据帧的最后一列。考虑到_最后一列的索引取决于df,一个不包含关键字的解决方案对我来说不起作用

这就是我所拥有的:

df_1

df_2

这就是我想要的: df_1

df_2

您可以使用:

columns=df.columns.tolist()
columns.append(columns.pop([df.columns.tolist().index(key) for key in df.columns if 'last' in key][0]))
df.reindex(columns=columns)

包含最后一个的名称将被删除并添加alfinal。

HI,请提供sortedk,key=lambda s:“\u last”in s?”rafaelc是的,似乎这就是OP想要的
column_1        column_2          something_last          column_3
    a                b                 c                  d
column_1        column_2          column_3           column_last
    a                c                 d                  b
column_1        column_2          column_3          something_last
    a                b                 d                  c
columns=df.columns.tolist()
columns.append(columns.pop([df.columns.tolist().index(key) for key in df.columns if 'last' in key][0]))
df.reindex(columns=columns)