You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何指定列列表将多列移至Pandas DataFrame末尾

移动指定列到Pandas DataFrame末尾

原DataFrame

index  a   b   c
1      2   3   4
2      3   4   5

需求

指定列列表 l = ['a', 'b'],将这两列移至DataFrame末尾,无需手动指定所有列。

解决方案

通过筛选出不需要移动的列,再将其与目标列拼接成新的列顺序,重新索引DataFrame即可:

import pandas as pd

# 构造原DataFrame
df = pd.DataFrame({'a': [2, 3], 'b': [3, 4], 'c': [4, 5]}, index=[1, 2])

# 指定要移动的列
l = ['a', 'b']

# 获取无需移动的列
remaining_cols = [col for col in df.columns if col not in l]

# 重新排列列顺序
df = df[remaining_cols + l]

执行结果

index  c   a   b
1      4   2   3
2      5   3   4

内容的提问来源于stack exchange,提问作者user308827

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 03:22:37