Skip to main content
 首页 » 编程设计

python之重新排序 Pandas 数据框中的某些列

2025年05月04日12birdshome

我有一个数据框,按以下顺序包含以下列;

'2','4','9','A','1','B','C' 

我希望前 3 列是 ABC,但其余的无关紧要。

输出:

'A','B','C','3','2','9'... and so on 

这可能吗?

(有 100 列,所以我不能把它们全部放在一个列表中

请您参考如下方法:

您可以尝试这样重新排序:

first_cols = ['A','B','C'] 
last_cols = [col for col in df.columns if col not in first_cols] 
 
df = df[first_cols+last_cols]