I am currently working with a DataFrame from a dictionary that I wish to reformat. The dictionary looks like this:
transactionDetails = {"paymentStatus":["COMPLETED", "REFUNDED", "COMPLETED"],
"address":["123 Fake Street", "123 Example Street", "123 Top Secret"],
"item":["Apple", "Banana", "Orange"],
"transactionID":["2132123", "54654645", "56754646"],
"orderTime":["14:55", "15:10", "23:11"],
"email":["[email protected]", "[email protected]", "[email protected]"],
"refundNotes":[],
"notes": []}
The dictionary has been written to the DataFrame in the following way:
df = pd.DataFrame.from_dict(transactionDetails, orient='index')
This currently outputs the DataFrame as follows:
0 1 2
paymentStatus COMPLETED REFUNDED COMPLETED
address 123 Fake Street 123 Example Street 123 Top Secret
item Apple Banana Orange
transactionID 2132123 54654645 56754646
orderTime 14:55 15:10 23:11
email [email protected] [email protected] [email protected]
refundNotes None None None
notes None None None
I would like to present the data vertically in the following way:
paymentStatus COMPLETED
address 123 Fake Street
item Apple
transactionID 2132123
orderTime 14:55
email [email protected]
refundNotes None
notes None
paymentStatus COMPLETED
address 123 Example Street
item Banana
transactionID 54654645
orderTime 15:10
email [email protected]
refundNotes None
notes None
etc
PS: I have tried using .stack(), but that resulted in the following output which is not what I'm after:
paymentStatus 0 COMPLETED
1 REFUNDED
2 COMPLETED
address 0 123 Fake Street
1 123 Example Street
2 123 Top Secret
item 0 Apple
1 Banana
2 Orange
transactionID 0 2132123
1 54654645
2 56754646
orderTime 0 14:55
1 15:10
2 23:11
email 0 [email protected]
1 [email protected]
2 [email protected]
Thanks!
df.stack().sort_index(level=1)?reset_indexafter.df.stack().sort_index(level=1).reset_index(level=1, drop=True)