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

Pandas DataFrame多列批量重命名及列偏移调整问题

Got it, dealing with column misalignment on a huge DataFrame is a pain—especially with 300 columns, renaming each one manually is totally out of the question. Here are two efficient fixes for you:

Fix 1: Correct the already loaded misaligned DataFrame

Your current DataFrame has columns shifted right by one, with a final column full of NaNs. We can fix this in two quick slicing steps:

# Extract the correct column names (shift left, drop the last empty one)
new_columns = df.columns[1:-1].tolist()
# Slice the data to remove the first shifted column and last NaN column, then set new names
df = df.iloc[:, 1:-1]
df.columns = new_columns

Breakdown:

  • df.columns[1:-1]: Grabs all column names starting from the second one up to the second-to-last one, which are your actual desired column headers.
  • df.iloc[:, 1:-1]: Removes the first extraneous column (from the shift) and the final all-NaN column from your data.
  • Assigning new_columns to the sliced DataFrame aligns everything perfectly.

Fix 2: Avoid misalignment entirely when reading the CSV

Looking at your CSV content, the first line is a comment header, and the second line is your actual column names. You can read the CSV correctly in one step by skipping that first comment line and letting pandas use the second line as headers:

import pandas as pd

# Skip the first comment line, use the second line as column headers
df = pd.read_csv('myCSV.csv', skiprows=1)

This reads the data directly into a properly aligned DataFrame, no post-processing needed. For your sample CSV, the output of df.head(2) will look exactly like what you want:

LocalTimeTempTempDiffTempNormalDewPointCloud CoverFeelsLikeTemp
5/16/2018 0:0068-3.3857.566.9210068
5/16/2018 1:0066.92-3.8955.2266.9210066.92

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:13:11