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

如何将DataFrame中含“___”的指定列移至末尾且保留其他列顺序?

将含“___”的DataFrame列移至末尾的实现方法

核心思路

把DataFrame的列拆分为两组:

  • 不含___标识的列:完全保留原有顺序
  • 含___标识的列:保留自身原有相对顺序
    最后将两组列拼接,把含标识的列放在末尾。

代码实现

假设你的DataFrame名为df,直接用以下代码即可完成列重排:

# 筛选不含"___"的列,维持原顺序
non_target_cols = [col for col in df.columns if '___' not in col]
# 筛选含"___"的列,维持原顺序
target_cols = [col for col in df.columns if '___' in col]
# 重新排列DataFrame的列
df_reordered = df[non_target_cols + target_cols]

效果验证

如果原DataFrame的列顺序为:
['normal_col1', 'aaa___1', 'normal_col2', 'abc_2___2', 'normal_col3']
执行代码后,列顺序会变为:
['normal_col1', 'normal_col2', 'normal_col3', 'aaa___1', 'abc_2___2']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 16:10:29