Skip to content
Advertisement

inserting missing categories and dates in pandas dataframe

I have the following data frame. I want to add in all score levels (high, mid, low), for each group (a, b, c, d), for all dates (there are two dates – 2020-06-01 and 2020-06-02)

JavaScript

I can add in the score categories for all subjects with the following, but i am having trouble adding date in as well

JavaScript

the expected output would be this : so there are 6 rows per subject, 3 rows for each date, and one row for each score category. The count is then filled in with np.nan (or zero is fine) where the data points are missing

JavaScript

any advice would be great, thank you

Advertisement

Answer

Your solution is possible modify with add date columns by unique values, this solution working if not unique triples date, group, score in input data:

JavaScript

Solution with reindex by 3 level MultiIndex is similar:

JavaScript

With one call unstack and one call stack is possible use, but is necessary all unique values cats have to exist in input data:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement