我有一个 Pandas 数据帧,其中包含以下信息:
如果我们看时间戳"16229767",有两个托盘正在使用:托盘3和托盘5。 托盘5的每个位置都被检测到了。 然而,由于位置2缺失数据,托盘3的数据是不完整的。
我想通过编程方式来修复并添加这一行。
我应该如何查找并添加丢失的行到数据框中?
- 每个时间戳有一定数量的托盘(1-4之间),共有8个托盘可供选择。(所以每个时间戳最多有4个托盘。)
- 每个托盘包含4个位置。
df =
timestamp t_idx position error type SNR
0 16229767 5 2 1 T1 123
1 16229767 5 1 0 T1 123
3 16229767 5 3 0 T1 123
4 16229767 5 4 0 T1 123
5 16229767 3 3 1 T9 38
6 16229767 3 1 0 T9 38
7 16229767 3 4 0 T9 38
8 29767162 7 1 0 T4 991
9 29767162 7 4 1 T4 991
如果我们看时间戳"16229767",有两个托盘正在使用:托盘3和托盘5。 托盘5的每个位置都被检测到了。 然而,由于位置2缺失数据,托盘3的数据是不完整的。
我想通过编程方式来修复并添加这一行。
10 16229767 3 2 1 T9 38
11 29767162 7 2 1 T4 991
12 29767162 7 3 1 T4 991
我不确定如何正确处理缺失的值。我目前采用的是比较幼稚的方法:
timestamps = df['timestamp'].unique()
for ts in timestamps:
tray_ids = df.loc[df['timestamp'] == timestamps ]["Tray ID"].unique()
for t_id in tray_ids:
# For timestamp and tray id: Each position (1 to 4) should exist once!
# df.loc[(df['timestamp'] == ts) & (df['Tray ID'] == t_id)]
# if not, append the position on the tray and set error to 1
我应该如何查找并添加丢失的行到数据框中?
===
编辑: 我在简化我的示例时遗漏了一些相关信息: 还存在其他列,新生成的行应每个托盘拥有相同的内容。通过添加两列使其更加清晰。
此外,有一个关于错误的问题:对于每一个要添加的行,错误应该自动为1(无逻辑可言)。
error
值是如何设置的? - Corralien