Reputation: 1033
I tried to create two sub-columns under one column. First I created a DataFrame from a Dictionary
Destination = ['Milan', 'London','Lisbon','Milan','Mumbai', 'Lisbon', 'Porto', 'Lisbon']
Product_Revenue = [542,332,135,524,232, 522, 122, 122]
data_frame = {'Destination' : [i for i in Destination],
'Revenue': [j for j in Product_Revenue]}
pd.DataFrame(data_frame)
Second, I wanted to add Booking
as the main column so that Destination
and Revenue
becomes sub-column of it. But I got Destination
and Revenue
in the index and even as a list.
data_frame = {'Booking': {'Destination' : [i for i in Destination],
'Revenue': [j for j in Product_Revenue]}}
pd.DataFrame(data_frame)
Booking
Destination [Milan, London, Vienna, Milan, Mumbai, Paris, ...
Revenue [542, 332, 135, 524, 232, 522, 122, 122]
The Destination
and Revenue
is showing in rows.
How can I write each value of both in parallel as the previous one?
A future goal is to add Users
as the index. So, the final dataframe would be User
Vs Booking
Data where Destination
and Price
values would vary for each user.
Any help would be highly appreciated.
Upvotes: 1
Views: 446
Reputation: 1422
The question is a bit confusing. If you are looking to build a DF with columns as multiindex where the first level is 'Booking' and the second level is [Destination, Revenue], the keys should be defined as tuples:
Destination = ['Milan', 'London','Lisbon','Milan','Mumbai', 'Lisbon', 'Porto', 'Lisbon']
Product_Revenue = [542,332,135,524,232, 522, 122, 122]
data_frame = {('Booking', 'Destination') : Destination,
('Booking', 'Revenue'): Product_Revenue}
pd.DataFrame(data_frame)
Output:
Booking
Destination Revenue
0 Milan 542
1 London 332
2 Lisbon 135
3 Milan 524
4 Mumbai 232
5 Lisbon 522
6 Porto 122
7 Lisbon 122
Upvotes: 1