I have a pandas dataframe with three columns, the first two columns are factors, and the third column contains counts. I want to 'explode' or 'unroll' the dataframe so that instead of having one line for each unique element of first column, second column, I have the number of rows equal to the sum of the counts column, where each new line has a unique and incrementing identifier number, but I want a separate counter for each level within one of the two columns. Note, this question is similar to How can I 'unroll' a pandas dataframe? which I asked yesterday, but has some additional complications that i failed to recognize the first time, and i'm unable to generalize (for myself) how to extend upon it.
Here are the data frames
data = [['van', 'bc', 1], ['abb', 'bc', 3], ['vic','bc',3], ['cal', 'ab', 1], ['edm', 'ab', 2], ['cal','ab', 2], ['van', 'bc', 1]]
df = pd.DataFrame(data, columns = ['city', 'state', 'count'])
and I want to turn that into this
data = [['van', 'bc', 'dr0001'], ['abb', 'bc', 'dr0002'], ['abb', 'bc', 'dr0003'], ['abb', 'bc', 'dr0004'], ['vic', 'bc', 'dr0005'], ['vic', 'bc', 'dr0006'], ['vic', 'bc', 'dr0007'], ['cal', 'ab', 'dr0001'], ['edm', 'ab', 'dr0002'], ['edm', 'ab', 'dr0003'], ['edm', 'ab', 'dr0004'], ['edm', 'ab', 'dr0005'], ['van', 'bc', 'dr0008']]
df = pd.DataFrame(data, columns = ['city', 'state', 'id'])
Thanks