I am working on a project using Pandas, and am running into trouble when trying to condense similar objects.
I have a dataframe with the columns Product ID, Currency, Price, and Book.
There are identical products in the Product ID category, such as X11, X23, X25, and so on. There are 3 instances of each, each with one of 3 currencies, with a price, and with one of 3 Books.
I want to have a dataframe where each instance is one product, with whatever books they appear in, and the 4 prices of the 4 currencies contained in each instance.
This is an example of what the Dataframe looks like now:
df = pd.DataFrame({'Product ID' : ['X11' ,'X11', 'X11', 'X23', 'X23', 'X23', 'X25', 'X25'],
'Currency' : ['USD', 'EUR', 'GBP', 'USD', 'EUR', 'GBP', 'EUR', 'GBP'],
'Price' : [100, 90, 90, 200, 180, 180, 90, 90],
'Book' : ['America', 'Canada', 'Mexico', 'America', 'Canada', 'Mexico', 'Canada', 'Mexico']})
df
Book Currency Price Product ID
0 America USD 100 X11
1 Canada EUR 90 X11
2 Mexico GBP 90 X11
3 America USD 200 X23
4 Canada EUR 180 X23
5 Mexico GBP 180 X23
6 Canada EUR 90 X25
7 Mexico GBP 90 X25
Ultimately It will be converted to a JSON file that has all of that data in one instance, however before that happens I need to condense the identical products. See below for an example of what the final JSON converted object will look like.
What would be the best way of achieving this? I don't fully understand groupby, as some similar questions have suggested, and I haven't seen a question that answers how to do this. The actual conversion itself shouldn't be too hard once I have identical products only occurring in one instance, but with all of the price and book data.
Any help is much appreciated.