我想做的是每当缺少一行时将记录插入到数据集中。
如果您查看上面的数据集,它包含 3 列属性,然后是 2 个数值。第三列 TTF 是增量的,不应跳过任何值。在此示例中,缺少显示在底部的 2 行。因此,我希望我的代码执行的操作是将这两行插入到结果集中(即计算机 - 显示器缺少 5 的 TTF,而电视 - 电源缺少 6 的 TTF。我会将修复值设置为 0,并且运行总计值与上一行相同)。
我想我可以通过拆分列名称并递归遍历前 2 个列名称,然后遍历第三个列名称 1 到 8 来实现它。
for i in range(len(Product)):
for j in range(len(Module)):
for k in range(1, 8):
# Check if the Repair value is there if not make it 0
# If Repair value is missing, look up previous Running Total
这看起来是最好的方法吗?任何有关实际代码来完成此任务的帮助将不胜感激。
编辑:这是 DF 中的代码读取,因为根据 Excel 屏幕截图,这似乎令人困惑。
>>> import pandas as pd
>>>
>>> df = pd.read_csv('minimal.csv')
>>>
>>> df
Product Module TTF Repair Running Total
0 Computer Display 1 3 3
1 Computer Display 2 2 5
2 Computer Display 3 1 6
3 Computer Display 4 5 11
4 Computer Display 6 4 15
5 Computer Display 7 3 18
6 Computer Display 8 2 20
7 Television Power Supply 1 7 7
8 Television Power Supply 2 6 13
9 Television Power Supply 3 4 17
10 Television Power Supply 4 5 22
11 Television Power Supply 5 6 28
12 Television Power Supply 7 7 35
13 Television Power Supply 8 8 43
最佳答案
让我们使用reindex
为np.arange
序列中缺失的数字创建新的TTF:
df = pd.DataFrame({'Product':['Computer']*7 + ['Television']*7,'Module':['Display']*7 + ['Power Supply']*7,
'TTF':[1,2,3,4,6,7,8,1,2,3,4,5,7,8],'Repair':np.random.randint(1,8,14)})
df['Running Total'] = df['Repair'].cumsum()
print(df)
输入数据框:
Module Product Repair TTF Running Total
0 Display Computer 6 1 6
1 Display Computer 2 2 8
2 Display Computer 2 3 10
3 Display Computer 4 4 14
4 Display Computer 2 6 16
5 Display Computer 3 7 19
6 Display Computer 6 8 25
7 Power Supply Television 3 1 28
8 Power Supply Television 3 2 31
9 Power Supply Television 5 3 36
10 Power Supply Television 6 4 42
11 Power Supply Television 4 5 46
12 Power Supply Television 2 7 48
13 Power Supply Television 2 8 50
df_out = df.set_index('TTF').groupby(['Product','Module'], group_keys=False).apply(lambda x: x.reindex(np.arange(1,9)))
df_out['repair'] = df_out['Repair'].fillna(0)
df_out = df_out.ffill().reset_index()
print(df_out)
输出:
TTF Module Product Repair Running Total repair
0 1 Display Computer 6.0 6.0 6.0
1 2 Display Computer 2.0 8.0 2.0
2 3 Display Computer 2.0 10.0 2.0
3 4 Display Computer 4.0 14.0 4.0
4 5 Display Computer 4.0 14.0 0.0
5 6 Display Computer 2.0 16.0 2.0
6 7 Display Computer 3.0 19.0 3.0
7 8 Display Computer 6.0 25.0 6.0
8 1 Power Supply Television 3.0 28.0 3.0
9 2 Power Supply Television 3.0 31.0 3.0
10 3 Power Supply Television 5.0 36.0 5.0
11 4 Power Supply Television 6.0 42.0 6.0
12 5 Power Supply Television 4.0 46.0 4.0
13 6 Power Supply Television 4.0 46.0 0.0
14 7 Power Supply Television 2.0 48.0 2.0
15 8 Power Supply Television 2.0 50.0 2.0
关于python - 如何将缺失的行插入到该数据集中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48102138/