如何用Pandas填写范围内的缺失值? [英] How can I fill in a missing values in range with Pandas?
问题描述
我有一个数据集,其中包含许多如下所示的值.
I have a dataset with a number of values like below.
>>> a.head()
value freq
3 9 1
2 11 1
0 12 4
1 15 2
我需要在value列中的整数之间填写值.例如,我需要在9&之间插入一个新行. 11填充零,然后在12-15之间再填充两个.最终结果应该是9-15的数据集,其中缺失"行全为零.
I need to fill in the values between the integers in the value column. For example, I need to insert one new row between 9 & 11 filled with zeroes, then another two between 12-15. The end result should be the dataset with 9-15 with 'missing' rows as zeroes across the board.
总有没有在不替换数据的情况下在特定位置插入新行的情况?我发现的唯一方法是在一个位置上切片数据帧,然后追加新行并连接其余部分.
Is there anyway to insert a new row at an specific location without replacing data? The only methods I've found involve slicing the dataframe at a location then appending a new row and concatenating the remainder.
更新:索引完全无关紧要,所以不必担心.
UPDATE: The index is completely irrelevant so don't worry about that.
推荐答案
您没有说索引会发生什么,所以我认为它并不重要.
You didn't say what should happen to your Index, so I'm assuming it's unimportant.
In [12]: df.index = df['value']
In [15]: df.reindex(np.arange(df.value.min(), df.value.max() + 1)).fillna(0)
Out[15]:
value freq
value
9 9 1
10 0 0
11 11 1
12 12 4
13 0 0
14 0 0
15 15 2
这篇关于如何用Pandas填写范围内的缺失值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!