使用循环在数据框中重命名列
说我有一个数据框(DF),如下所示
它的列3列都命名为Same(L,L,L,L)。我想根据其列位置将列命名为L1,L2和L3。我希望选择的外观如下所示。
我可以使用代码做到这一点 df.columns = ['input','l1','l2','l3']
但是,我相信必须在所有列(或选择的列)上循环(或选择的列),以便为任何DataFrame自动化这一点。
非常感谢您的回复。
Say I have a dataframe (df) which looks as given below
It has columns 3 columns all named the same (L, L, L). I want to rename the columns as L1, L2 and L3 based on their column position. I expect the optput to look as given below.
I can do it using the code
df.columns = ['Input', 'L1', 'L2', 'L3']
However, I believe a way must be available where I loop over all columns (or chosen columns) so that this can be automated for any dataframe.
Many thanks in advance for the responses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
You use