![]() |
Converting column of values into muliple columns of counts - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Converting column of values into muliple columns of counts (/thread-41531.html) |
Converting column of values into muliple columns of counts - highland44 - Feb-01-2024 Hi everyone. I have a dataframe in Python that has the following format: Cyc MA S1 1 S1 5 S2 7 S3 2 S3 6 S3 4 S4 13 S5 1 S5 1 I'm trying to convert it into the following format, where each cell represents the count of the number of times each Cyc value was each MA value. This would require making many new columns (basically between 1 and 13, the lowest and highest values of MA from the original table). Cyc MA1 MA2 MA3 MA4 MA5 MA6 MA7 MA8 MA9 M10 M11 M12 M13 S1 1 0 0 0 1 0 0 0 0 0 0 0 0 S2 0 0 0 0 0 0 1 0 0 0 0 0 0 S3 0 1 0 1 0 1 0 0 0 0 0 0 0 S4 0 0 0 0 0 0 0 0 0 0 0 0 1 S5 2 0 0 0 0 0 0 0 0 0 0 0 0 I've tried a few things with "melting" the data frame and doing "group.by sums" -- but I was unable to entirely replicate this slightly more complicated scenario (i.e. would only solve a part of the problem). Any suggestions or pointers would be so helpful. Thank you. |