我有一个数据集
x text
1 one
2 two
4 four
5 five
7 seven
现在我要输出为
x text
1 one
2 two
3 null
4 four
5 five
6 null
7 seven
答案 0 :(得分:2)
使用reindex
:
#if x is column
#df = df.set_index('x').reindex(np.arange(df.index.min(), df.index.max() + 1))
#if x is index
df = df.reindex(np.arange(df.index.min(), df.index.max() + 1))
print (df)
text
x
1 one
2 two
3 NaN
4 four
5 five
6 NaN
7 seven