I have a dataframe df, for example:
A = [["John", "Sunday", 6], ["John", "Monday", 3], ["John", "Tuesday", 2], ["Mary", "Sunday", 6], ["Mary", "Monday", 4], ["Mary", "Tuesday", 7]]
df = pandas.DataFrame(A, columns=["names", "dates", "times"])
And I want to reshape it so that, instead of three columns, I can create a matrix where the first column indexes the rows, the second column indexes the columns, and the third column becomes the matrix value, something like:
B = [["John", 6, 3, 2], ["Mary", 6, 4, 7]]
df2 = pandas.DataFrame(B, columns=["names", "Sunday", "Monday", "Tuesday"])
or even better:
B = numpy.asarray(B)
B = pandas.DataFrame(B)
How do I transform A into B?
I have created a double for loop, but in my case df is very large and it takes a very long time. Is there a better way to do it?
This is not just a reshape, since A has 18 values and B has 8