gpt4 book ai didi

python - 删除几列后从 0 开始重新分配列标签

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:16 25 4
gpt4 key购买 nike

我通过以下命令删除了一些重复的列。

columns = XY.columns[:-1].tolist()
XY1 = XY.drop_duplicates(subset=columns,keep='first').

结果如下:

Combined Series shape :  (100, 4)                            
Combined Series: 1 222 223 0
0 0 0 0 1998.850000
1 0 0 0 0.947361
2 0 0 0 0.947361
3 0 0 0 0.947361
4 0 0 0 0.947361

Now the columns is labelled 1 222 223 0 (0 label at the end is because of concat with another df !!) I want the columns to be re-labelled from index 0 onwards. How'll I do it?

最佳答案

因此,首先使用您想要的映射创建一个字典

trafo_dict = {x:y for x,y in zip( [1,222,223,0],np.linspace(0,3,4))}

然后您需要重命名列。这可以通过 pd.DataFrame.rename 来完成:

  XY1 = XY1.rename(columns=trafo_dict)

编辑:如果您希望它以更通用的方式使用:

np.linspace(0,XY1.shape[1]-1,XY1.shape[1])

关于python - 删除几列后从 0 开始重新分配列标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49444445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com