Python Pandas add rows based on missing sequential values in a timeseries

一世执手 提交于 2020-01-21 07:19:26

问题


I'm new to python and struggling 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 want to complete the missing years creating rows with empty values, like this:

    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 that in Python? (I wanna do that so I can plot Values without skipping years)


回答1:


I would create a new dataframe that has Year as an Index and includes the entire date range that you need to cover. Then you can simply set the values across the two dataframes, and the index will make sure that they correct rows are matched (I've had to use fillna to set the missing years to zero, by default they will be set to 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

Finally you can use reset_index if you don't want Year as your 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


来源:https://stackoverflow.com/questions/30564610/python-pandas-add-rows-based-on-missing-sequential-values-in-a-timeseries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!