如何在Pandas DataFrame中基于actual列创建actual_new列
在Pandas中基于actual列生成actual_new列
规律分析
从示例数据可以看出:
- 所有
actual_new的值来自一个固定基准序列,这个序列是按actual值对应的最早start time排序得到的唯一actual值列表 - 每个
actual分组内的行按start time排序后,依次对应基准序列的第1到第n个元素(n为基准序列长度)
实现步骤
- 将时间列转换为datetime类型,确保排序逻辑正确
- 生成基准序列:提取每个
actual对应的最早start time,按时间排序后得到基准actual值列表 - 按
actual分组并排序,给组内每行分配索引,根据索引映射基准序列值生成actual_new
完整代码
import pandas as pd # 构造示例DataFrame(实际使用时替换为你的数据读取逻辑) data = [ ["4/1/2022 20:00", "4/1/2022 21:00", 0.749123, 0.749123], ["4/1/2022 21:00", "4/1/2022 22:00", 0.749123, 0.770175], ["4/1/2022 22:00", "4/1/2022 23:00", 0.749123, 0.725439], ["4/1/2022 23:00", "4/2/2022 0:00", 0.749123, 0.659649], ["4/2/2022 0:00", "4/2/2022 1:00", 0.749123, 0.245614], ["4/2/2022 1:00", "4/2/2022 2:00", 0.749123, 0.078947], ["4/1/2022 21:00", "4/1/2022 22:00", 0.770175, 0.749123], ["4/1/2022 22:00", "4/1/2022 23:00", 0.770175, 0.770175], ["4/1/2022 23:00", "4/2/2022 0:00", 0.770175, 0.725439], ["4/2/2022 0:00", "4/2/2022 1:00", 0.770175, 0.659649], ["4/2/2022 1:00", "4/2/2022 2:00", 0.770175, 0.245614], ["4/2/2022 2:00", "4/2/2022 3:00", 0.770175, 0.078947], ["4/1/2022 22:00", "4/1/2022 23:00", 0.725439, 0.749123], ["4/1/2022 23:00", "4/2/2022 0:00", 0.725439, 0.770175], ["4/2/2022 0:00", "4/2/2022 1:00", 0.725439, 0.725439], ["4/2/2022 1:00", "4/2/2022 2:00", 0.725439, 0.659649], ["4/2/2022 2:00", "4/2/2022 3:00", 0.725439, 0.245614], ["4/2/2022 3:00", "4/2/2022 4:00", 0.725439, 0.078947], ["4/1/2022 23:00", "4/2/2022 0:00", 0.659649, 0.749123], ["4/2/2022 0:00", "4/2/2022 1:00", 0.659649, 0.770175], ["4/2/2022 1:00", "4/2/2022 2:00", 0.659649, 0.725439], ["4/2/2022 2:00", "4/2/2022 3:00", 0.659649, 0.659649], ["4/2/2022 3:00", "4/2/2022 4:00", 0.659649, 0.245614], ["4/2/2022 4:00", "4/2/2022 5:00", 0.659649, 0.078947], ["4/2/2022 0:00", "4/2/2022 1:00", 0.245614, 0.749123], ["4/2/2022 1:00", "4/2/2022 2:00", 0.245614, 0.770175], ["4/2/2022 2:00", "4/2/2022 3:00", 0.245614, 0.725439], ["4/2/2022 3:00", "4/2/2022 4:00", 0.245614, 0.659649], ["4/2/2022 4:00", "4/2/2022 5:00", 0.245614, 0.245614], ["4/2/2022 5:00", "4/2/2022 6:00", 0.245614, 0.078947], ["4/2/2022 1:00", "4/2/2022 2:00", 0.078947, 0.749123], ["4/2/2022 2:00", "4/2/2022 3:00", 0.078947, 0.770175], ["4/2/2022 3:00", "4/2/2022 4:00", 0.078947, 0.725439], ["4/2/2022 4:00", "4/2/2022 5:00", 0.078947, 0.659649], ["4/2/2022 5:00", "4/2/2022 6:00", 0.078947, 0.245614], ["4/2/2022 6:00", "4/2/2022 7:00", 0.078947, 0.078947] ] df = pd.DataFrame(data, columns=["start time", "end time", "actual", "actual_new"]) # 1. 转换时间列为datetime类型 df["start time"] = pd.to_datetime(df["start time"]) df["end time"] = pd.to_datetime(df["end time"]) # 2. 生成基准序列:按actual对应的最早start time排序 actual_min_start = df.groupby("actual")["start time"].min().reset_index() base_sequence = actual_min_start.sort_values("start time")["actual"].tolist() # 3. 分组排序并生成actual_new df = df.sort_values(["actual", "start time"]) df["group_index"] = df.groupby("actual").cumcount() df["actual_new_generated"] = df["group_index"].apply(lambda x: base_sequence[x]) # 验证结果(可选) print(df[["actual", "actual_new", "actual_new_generated"]].equals(df[["actual", "actual_new", "actual_new"]]))
说明
- 基准序列会自动根据数据中的时间顺序生成,无需手动指定
- 代码中生成的
actual_new_generated列与示例中的actual_new完全一致 - 如果你的数据中每个
actual分组的行数等于基准序列长度,该方法完全适用
内容的提问来源于stack exchange,提问作者Rajan Kumar Yadav
相关产品推荐
相关产品推荐

