gpt4 book ai didi

用于滚动窗口索引的 Pandas 编程模型

转载 作者:行者123 更新时间:2023-12-01 05:16:57 25 4
gpt4 key购买 nike

我需要关于编程模式和 DataFrame 用于数据的建议。我们有数千个小 ASCII 文件,它们是粒子跟踪实验的结果(有关详细信息,请参见 www.openptv.net)。每个文件都是在该时间实例中识别和跟踪的粒子列表。文件名就是帧数。例如:

ptv_is.10000(即帧号 10000)

prev next   x    y   z   
-1 5 0.0 0.0 0.0
0 0 1.0 1.0 1.0
1 1 2.0 2.0 2.0
2 2 3.0 3.0 3.0
3 -2 4.0 4.0 4.0

ptv_is.10001(即下一个时间范围,10001)
1    2      1.1 1.0 1.0 
2 8 2.0 2.0 2.0
3 14 3.0 3.0 3.0
4 -2 4.0 4.0 4.0
-1 3 1.5 1.12 1.32
0 -2 0.0 0.0 0.0

ASCII 文件的列是:prev - 上一帧粒子的行号,next 是下一帧粒子的行号,x,y,z 是粒子的坐标。如果 'prev' 的行索引为 -1 - 粒子出现在当前帧中并且没有及时链接。如果 'next' 是 -2,那么粒子在时间上没有向前的链接,轨迹在这一帧结束。

因此,我们将这些文件读入具有相同列标题的单个 DataFrame 中,并添加时间索引,即帧编号
prev next   x    y   z   time
-1 5 0.0 0.0 0.0 10000
0 0 1.0 1.0 1.0 10000
1 1 2.0 2.0 2.0 10000
2 2 3.0 3.0 3.0 10000
3 -2 4.0 4.0 4.0 10000

1 2 1.1 1.0 1.0 10001
2 8 2.0 2.0 2.0 10001
3 14 3.0 3.0 3.0 10001
4 -2 4.0 4.0 4.0 10001
-1 3 1.5 1.12 1.32 10001
0 -2 0.0 0.0 0.0 10001

现在我发现很难找到使用 DataFrame 的最佳方式。如果我们可以添加一个额外的列,称为trajectory_id,我们以后就可以通过时间(在单个时间实例中创建粒子的子组并学习它们的空间分布)或通过trajectory_id重新索引这个DataFrame,然后创建轨迹(或链接粒子并了解它们在空间中的时间演化,例如 x(t), y(t), z(t) 对于相同的trajectory_id)。

如果输入是:
prev next   x    y   z   time
-1 5 0.0 0.0 0.0 10000
0 0 1.0 1.0 1.0 10000
1 1 2.0 2.0 2.0 10000
2 2 3.0 3.0 3.0 10000
3 -2 4.0 4.0 4.0 10000

1 2 1.1 1.0 1.0 10001
2 8 2.0 2.0 2.0 10001
3 14 3.0 3.0 3.0 10001
4 -2 4.0 4.0 4.0 10001
-1 3 1.5 1.12 1.32 10001
0 -2 0.0 0.0 0.0 10001

那么我需要的结果是:
prev next   x    y   z   time   trajectory_id
-1 5 0.0 0.0 0.0 10000 1
0 0 1.0 1.0 1.0 10000 2
1 1 2.0 2.0 2.0 10000 3
2 2 3.0 3.0 3.0 10000 4
3 -2 4.0 4.0 4.0 10000 -999

1 2 1.1 1.0 1.0 10001 2
2 8 2.0 2.0 2.0 10001 3
3 14 3.0 3.0 3.0 10001 4
-1 -2 4.0 4.0 4.0 10001 -999
-1 3 1.5 1.1 1.3 10001 5
0 -2 0.0 0.0 0.0 10001 1

意思是:
prev next    x    y   z   time   trajectory_id
-1 5 0.0 0.0 0.0 10000 1 < - appeared first time, new id
0 0 1.0 1.0 1.0 10000 2 < - the same
1 1 2.0 2.0 2.0 10000 3 <- the same
2 2 3.0 3.0 3.0 10000 4 <- the same
3 -2 4.0 4.0 4.0 10000 -999 <- sort of NaN, there is no link in the next frame

1 2 1.1 1.0 1.0 10001 2 <- from row #1 in the time 10000, has an id = 2
2 8 2.0 2.0 2.0 10001 3 <- row #2 at previous time, has an id = 3
3 14 3.0 3.0 3.0 10001 4 < from row # 3, next on the row #14, id = 4
-1 -2 4.0 4.0 4.0 10001 -999 <- but linked, marked as NaN or -999
-1 3 1.5 1.1 1.3 10001 5 <- new particle, new id = 5 (new trajectory_id)
0 -2 0.0 0.0 0.0 10001 1 <- from row #0 id = 1

希望这能更好地解释我正在寻找的东西。唯一的问题是我不知道如何通过 DataFrame 表的行进行滚动功能,创建一个新的索引列,trajectory_id。

例如,带有列表的简单应用程序如下所示:

http://nbviewer.ipython.org/7020209

感谢有关 Pandas 使用的每一个提示,
亚历克斯

最佳答案

整洁的!这个问题贴近我的心;我也用 pandas for particle tracking .这与我正在处理的问题并不完全相同,但这里有一个未经测试的草图,提供了一些有用的 Pandas 习语。

results = []
first_loop = True
next_id = None
for frame_no, frame in pd.concat(list_of_dataframes).groupby('time'):
if first_loop:
frame['traj_id'] = np.arange(len(frame))
results.append(frame)
next_id = len(frame)
first_loop = False
continue
prev_frame = results[-1]
has_matches = frame['prev'] > 0 # boolean indexer
frame[has_matches]['traj_'id'] = prev_frame.iloc[frame[has_matches]['prev']]
count_unmatched = (~has_matches).sum()
frame[~has_matches]['traj_'id'] = np.arange(next_id, next_id + count_unmatched)
next_id += count_unmatched
results.append(frame)
pd.concat(results)

关于用于滚动窗口索引的 Pandas 编程模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19427382/

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