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

如何用while循环批量复制Dataframe的Power列并按规则命名

使用while循环批量复制DataFrame列示例

以下是用while循环批量复制"Power"列并生成指定命名新列的简单示例:

import pandas as pd

# 假设你的DataFrame名为df,且已包含"Power"列
# 设置需要复制的列数量
total_copies = 5  # 可根据需求修改这个数值
current = 1

while current <= total_copies:
    # 生成新列名并复制原列数据
    df[f"Power_{current}"] = df["Power"].copy()
    current += 1

说明:

  • 通过total_copies设定你需要复制的列总数
  • 循环从current=1开始,每次生成Power_1、Power_2这类格式的列名
  • 使用copy()确保新列是独立的数据副本,避免后续修改原列时影响新列

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 00:05:24