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

如何移除DataFrame部分列名开头的"sum of"以重命名列?

批量重命名以"sum of"开头的列名

Python(Pandas)方案

直接利用字符串替换逻辑,仅处理以sum of 开头的列名:

import pandas as pd

# 假设你的数据框名为df
df.columns = df.columns.str.replace(r'^sum of ', '', regex=True)
  • 正则表达式^sum of 精准匹配列名开头的"sum of "前缀,替换为空字符串后,非目标列名会保持原样。

R(dplyr)方案

借助批量重命名函数结合字符串处理实现:

library(dplyr)
library(stringr)

# 假设你的数据框名为df
df <- df %>%
  rename_with(~ str_remove(., "^sum of "), starts_with("sum of "))
  • starts_with("sum of ")筛选出需要修改的列,str_remove移除指定前缀,最终只更新目标列的名称。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 22:50:29