Filling the missing index and filling its value with 0

限于喜欢 提交于 2020-04-28 16:48:28

问题


I have a pandas dataframe with a column value as index number

      Sales
140   100
142   200
145   300

I want to fill the missing index and also want to fill the value of missing index with 0

      Sales
140   100
141   0
142   200
143   0
144   0
145   300

I also want to fill missing values as the missing index number like

     Week_num
140   140
142   142
145   145

      Week_Num
140   140
141   141
142   142
143   143
144   144
145   145

I request you to help me how to code this out?


回答1:


You can using reindex

df.reindex(list(range(df.index.min(),df.index.max()+1)),fill_value=0)
Out[471]: 
     Sales
140    100
141      0
142    200
143      0
144      0
145    300


来源:https://stackoverflow.com/questions/50690963/filling-the-missing-index-and-filling-its-value-with-0

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