Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧中插入缺少的行(可变索引范围)_Python_Pandas - Fatal编程技术网

Python 在数据帧中插入缺少的行(可变索引范围)

Python 在数据帧中插入缺少的行(可变索引范围),python,pandas,Python,Pandas,我有以下数据集: input_df = pd.DataFrame({'Product':['Computer']*5 + ['Television']*7, 'Module':['Display']*5 + ['Power Supply']*7, 'TTF':[1,2,3,4,6,1,2,3,4,5,7,8]}) output_df = pd.DataFrame({'Product':['Computer']*6 + ['

我有以下数据集:

input_df = pd.DataFrame({'Product':['Computer']*5 + ['Television']*7,
                   'Module':['Display']*5 + ['Power Supply']*7,
                 'TTF':[1,2,3,4,6,1,2,3,4,5,7,8]})
output_df = pd.DataFrame({'Product':['Computer']*6 + ['Television']*8,
                   'Module':['Display']*6 + ['Power Supply']*8,
                 'TTF':[1,2,3,4,5,6,1,2,3,4,5,6,7,8]})
我想插入缺少的行(索引4和11),以便获得以下数据集:

input_df = pd.DataFrame({'Product':['Computer']*5 + ['Television']*7,
                   'Module':['Display']*5 + ['Power Supply']*7,
                 'TTF':[1,2,3,4,6,1,2,3,4,5,7,8]})
output_df = pd.DataFrame({'Product':['Computer']*6 + ['Television']*8,
                   'Module':['Display']*6 + ['Power Supply']*8,
                 'TTF':[1,2,3,4,5,6,1,2,3,4,5,6,7,8]})
插入这些行最有效的方法是什么(我的真实数据集实际上相当大,有很多不同的类别)

我发现了一个相关的帖子: 然而,在这篇文章中,不同产品的索引范围并没有变化(总是[1到8],这与我的情况不同,在我的情况下,计算机的索引范围是[1到6],电视的索引范围是[1到8]。

与每组一起使用:

df = (input_df.set_index('TTF')
              .groupby(['Product','Module'], group_keys=False)
              .apply(lambda x: x.reindex(range(x.index.min(), 
                                               x.index.max() + 1), method='ffill'))
              .reset_index()
             )
print (df)
    TTF     Product        Module
0     1    Computer       Display
1     2    Computer       Display
2     3    Computer       Display
3     4    Computer       Display
4     5    Computer       Display
5     6    Computer       Display
6     1  Television  Power Supply
7     2  Television  Power Supply
8     3  Television  Power Supply
9     4  Television  Power Supply
10    5  Television  Power Supply
11    6  Television  Power Supply
12    7  Television  Power Supply
13    8  Television  Power Supply