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

标准化DataFrame列名:移除列名首尾空格的解决方案求助

解决DataFrame列名首尾空格问题
  • 无需合并所有DataFrame,直接遍历你的DataFrame列表,对每个DataFrame的列名做首尾空格去除即可:
    # 假设你的DataFrame列表名为df_list
    for df in df_list:
        df.columns = df.columns.str.strip()
    
  • 若不想修改原DataFrame,可生成新的清理后列表:
    cleaned_df_list = []
    for df in df_list:
        cleaned_df = df.copy()
        cleaned_df.columns = cleaned_df.columns.str.strip()
        cleaned_df_list.append(cleaned_df)
    
  • 要是需要处理列名中间的空格或其他空白字符,可扩展使用字符串替换:
    # 替换所有空格(包括中间)
    df.columns = df.columns.str.replace(" ", "")
    # 去掉所有空白字符(含制表符、换行符等)
    df.columns = df.columns.str.replace("\s+", "", regex=True)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 21:48:21