Pandas: create rows for each unique value of a column, even with missing data


Note: I had difficulty wording the title of my question, so if you can think of something better to help other people with a similar question, please let me know and I will change it.



Stored as a Pandas DataFrame



Notice that site b has no data for week 3



Essentially, I want to create rows for all of the unique combinations of week and site. If the original data doesn't have a vol for a week-site combo, then it gets a 0.



Using stack with unstack



You can use crosstab and stack:



Output:






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

大跃进

马相伯