在pandas数据帧中移动列

use*_*827 22 python dataframe pandas

我有以下数据帧:

   a  b   x  y
0  1  2   3 -1
1  2  4   6 -2
2  3  6   9 -3
3  4  8  12 -4
Run Code Online (Sandbox Code Playgroud)

如何移动列b和x,使它们成为数据框中的最后2列?我想按名称指定b和x,而不是其他列.

Ale*_*der 49

您可以通过指定其顺序直接重新排列列:

df = df[['a', 'y', 'b', 'x']]
Run Code Online (Sandbox Code Playgroud)

对于列标题是动态的较大数据帧,您可以使用列表推导来选择不在目标集中的每个列,然后将目标集追加到末尾.

>>> df[[c for c in df if c not in ['b', 'x']] 
       + ['b', 'x']]
   a  y  b   x
0  1 -1  2   3
1  2 -2  4   6
2  3 -3  6   9
3  4 -4  8  12
Run Code Online (Sandbox Code Playgroud)

为了使其更具防弹性,您可以确保目标列确实在数据框中:

cols_at_end = ['b', 'x']
df = df[[c for c in df if c not in cols_at_end] 
        + [c for c in cols_at_end if c in df]]
Run Code Online (Sandbox Code Playgroud)

  • 当有 10000 列时,我们可以更改列的顺序吗? (2认同)

Cha*_*ley 31

cols = list(df.columns.values) #Make a list of all of the columns in the df
cols.pop(cols.index('b')) #Remove b from list
cols.pop(cols.index('x')) #Remove x from list
df = df[cols+['b','x']] #Create new dataframe with columns in the order you want
Run Code Online (Sandbox Code Playgroud)


ROB*_*AT1 13

您可以使用以下方式.这很简单,但与Charlie Haley给出的好答案相似.

df1 = df.pop('b') # remove column b and store it in df1
df2 = df.pop('x') # remove column x and store it in df2
df['b']=df1 # add b series as a 'new' column.
df['x']=df2 # add b series as a 'new' column.
Run Code Online (Sandbox Code Playgroud)

现在,您的数据框最后包含"b"和"x"列.你可以在OSPY看到这个视频:https://youtu.be/RlbO27N3Xg4

  • 单行:`df['b'], df['x'] = df.pop('b'), df.pop('x')` (3认同)

Tan*_*uAD 11

简单的解决方案:

old_cols = df.columns.values 
new_cols= ['a', 'y', 'b', 'x']
df = df.reindex(columns=new_cols)
Run Code Online (Sandbox Code Playgroud)


Top*_*ner 10

此功能将重新排序您的列而不会丢失数据。任何省略的列都保留在数据集的中心:

def reorder_columns(columns, first_cols=[], last_cols=[], drop_cols=[]):
    columns = list(set(columns) - set(first_cols))
    columns = list(set(columns) - set(drop_cols))
    columns = list(set(columns) - set(last_cols))
    new_order = first_cols + columns + last_cols
    return new_order
Run Code Online (Sandbox Code Playgroud)

用法示例:

my_list = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
reorder_columns(my_list, first_cols=['fourth', 'third'], last_cols=['second'], drop_cols=['fifth'])

# Output:
['fourth', 'third', 'first', 'sixth', 'second']
Run Code Online (Sandbox Code Playgroud)

要分配给您的数据框,请使用:

my_list = df.columns.tolist()
reordered_cols = reorder_columns(my_list, first_cols=['fourth', 'third'], last_cols=['second'], drop_cols=['fifth'])
df = df[reordered_cols]
Run Code Online (Sandbox Code Playgroud)


Ste*_*veJ 5

另一种更通用的方法;

from pandas import DataFrame


def move_columns(df: DataFrame, cols_to_move: list, new_index: int) -> DataFrame:
    """
    This method re-arranges the columns in a dataframe to place the desired columns at the desired index.
    ex Usage: df = move_columns(df, ['Rev'], 2)   
    :param df:
    :param cols_to_move: The names of the columns to move. They must be a list
    :param new_index: The 0-based location to place the columns.
    :return: Return a dataframe with the columns re-arranged
    """
    other = [c for c in df if c not in cols_to_move]
    start = other[0:new_index]
    end = other[new_index:]
    return df[start + cols_to_move + end]
Run Code Online (Sandbox Code Playgroud)


Vru*_*run 5

类似于上面罗伯托·威廉姆斯·巴蒂斯塔的回答,但希望更强大一点:

df.insert(len(df.columns)-1, 'b', df.pop('b'))
df.insert(len(df.columns)-1, 'x', df.pop('x'))
Run Code Online (Sandbox Code Playgroud)

  • 这可能是最好的答案,至少如果您只想像我需要的那样在末尾放置一列,因为它避免了分配 @ROBBAT1 的答案中需要的临时 df1,并且还避免了手动重新排列名称列的列表。 (2认同)

小智 5

例如,要将列移动"name"到 df 中的第一列,您可以使用insert

column_to_move = df.pop("name")

# insert column with insert(location, column_name, column_value)

df.insert(0, "name", column_to_move)
Run Code Online (Sandbox Code Playgroud)

同样,如果您希望此列从头开始成为第三列:

df.insert(2, "name", column_to_move )
Run Code Online (Sandbox Code Playgroud)

  • 单行:`df.insert(0, 'name', df.pop('name'))` (5认同)
  • 这必须是人们所期望的最佳答案、最直观且友好的语法。谢谢@montxe (4认同)
  • 如果你想将该列移动到数据帧的末尾,你可以调用 `len(df.columns)` 作为位置整数,遗憾的是 Pandas 没有实现 -1 快捷方式。 (2认同)