Reputation: 123
I have another scenario, to replace the missing year and the corresponding columns with zeros. My dataframe 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 the missing year in between 2008 to 2014 ,
for example, 2009,2010, 2011 is missing how to fill these year in between and corresponding columns with zeros
Upvotes: 3
Views: 68
Reputation: 403208
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
Upvotes: 3