How to replace missing years in a data frame using python… here is a solution to the problem.
How to replace missing years in a data frame using python
I
have another case where I replace the missing year and corresponding column with zeros.
My data frame looks like this
Year Churn_Count Churn_Rate Customer_Count
2008 1071.0 0.800149 4114
2012 0.0 0.000000 6
2013 233.0 0.174075 824
2014 101.0 0.075458 410
I need to fill in the missing years between 2008 and 2014
For example
2009,2010,2011 Missing how to fill these years with zeros in the middle and corresponding columns
Solution
Use set_index
+ reindex
+ reset_index
:
df.set_index('Year').reindex(
np.arange(df. Year.min(), df. Year.max() + 1), fill_value=0
).reset_index()
Year Churn_Count Churn_Rate Customer_Count
0 2008 1071.0 0.800149 4114
1 2009 0.0 0.000000 0
2 2010 0.0 0.000000 0
3 2011 0.0 0.000000 0
4 2012 0.0 0.000000 6
5 2013 233.0 0.174075 824
6 2014 101.0 0.075458 410