Python Pandas add rows based on missing sequential values ​​in timers

I am new to python and trying to manipulate data in pandas library. I have a pandas database like this:

    Year  Value
0    91     1
1    93     4
2    94     7
3    95    10
4    98    13

And I want to complete the missing years by creating strings with empty values, for example:

    Year  Value
0    91     1
1    92     0
2    93     4
3    94     7
4    95    10
5    96     0
6    97     0
7    98    13

How do I do this in Python? (I want to do this so that I can depict values ​​without gaps)

+4
source share
1 answer

, Year in Index, , . , , ( fillna, , NaN):

df = pd.DataFrame({'Year':[91,93,94,95,98],'Value':[1,4,7,10,13]})
df.index = df.Year
df2 = pd.DataFrame({'Year':range(91,99), 'Value':0})
df2.index = df2.Year

df2.Value = df.Value
df2= df2.fillna(0)
df2
      Value  Year
Year             
91        1    91
92        0    92
93        4    93
94        7    94
95       10    95
96        0    96
97        0    97
98       13    98

, reset_index, :

df2.drop('Year',1).reset_index()

   Year  Value
0    91      1
1    92      0
2    93      4
3    94      7
4    95     10
5    96      0
6    97      0
7    98     13
+6

All Articles