728x90
In [ ]:
import pandas as pd
In [ ]:
# Multi-index DataFrame
sales_df = pd.read_csv('Online Retail.csv', encoding = 'unicode_escape')
sales_df.set_index(keys=['Country','InvoiceDate'], inplace = True)
sales_df
Out[ ]:
InvoiceNo | StockCode | Description | Quantity | UnitPrice | CustomerID | ||
---|---|---|---|---|---|---|---|
Country | InvoiceDate | ||||||
United Kingdom | 12/1/2010 8:26 | 536365 | 85123A | WHITE HANGING HEART T-LIGHT HOLDER | 6 | 2.55 | 17850.0 |
12/1/2010 8:26 | 536365 | 71053 | WHITE METAL LANTERN | 6 | 3.39 | 17850.0 | |
12/1/2010 8:26 | 536365 | 84406B | CREAM CUPID HEARTS COAT HANGER | 8 | 2.75 | 17850.0 | |
12/1/2010 8:26 | 536365 | 84029G | KNITTED UNION FLAG HOT WATER BOTTLE | 6 | 3.39 | 17850.0 | |
12/1/2010 8:26 | 536365 | 84029E | RED WOOLLY HOTTIE WHITE HEART. | 6 | 3.39 | 17850.0 | |
... | ... | ... | ... | ... | ... | ... | ... |
France | 12/9/2011 12:50 | 581587 | 22613 | PACK OF 20 SPACEBOY NAPKINS | 12 | 0.85 | 12680.0 |
12/9/2011 12:50 | 581587 | 22899 | CHILDREN'S APRON DOLLY GIRL | 6 | 2.10 | 12680.0 | |
12/9/2011 12:50 | 581587 | 23254 | CHILDRENS CUTLERY DOLLY GIRL | 4 | 4.15 | 12680.0 | |
12/9/2011 12:50 | 581587 | 23255 | CHILDRENS CUTLERY CIRCUS PARADE | 4 | 4.15 | 12680.0 | |
12/9/2011 12:50 | 581587 | 22138 | BAKING SET 9 PIECE RETROSPOT | 3 | 4.95 | 12680.0 |
541909 rows × 6 columns
In [ ]:
# the first level of indexing
sales_df.index.get_level_values(0)
Out[ ]:
Index(['United Kingdom', 'United Kingdom', 'United Kingdom', 'United Kingdom',
'United Kingdom', 'United Kingdom', 'United Kingdom', 'United Kingdom',
'United Kingdom', 'United Kingdom',
...
'France', 'France', 'France', 'France', 'France', 'France', 'France',
'France', 'France', 'France'],
dtype='object', name='Country', length=541909)
In [53]:
# the second level of indexing
sales_df.index.get_level_values(1)
Out[53]:
Index(['12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:26',
'12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:28',
'12/1/2010 8:28', '12/1/2010 8:34',
...
'12/9/2011 12:50', '12/9/2011 12:50', '12/9/2011 12:50',
'12/9/2011 12:50', '12/9/2011 12:50', '12/9/2011 12:50',
'12/9/2011 12:50', '12/9/2011 12:50', '12/9/2011 12:50',
'12/9/2011 12:50'],
dtype='object', name='InvoiceDate', length=541909)
In [54]:
# the name of the column/index
sales_df.index.get_level_values('InvoiceDate')
Out[54]:
Index(['12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:26',
'12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:26', '12/1/2010 8:28',
'12/1/2010 8:28', '12/1/2010 8:34',
...
'12/9/2011 12:50', '12/9/2011 12:50', '12/9/2011 12:50',
'12/9/2011 12:50', '12/9/2011 12:50', '12/9/2011 12:50',
'12/9/2011 12:50', '12/9/2011 12:50', '12/9/2011 12:50',
'12/9/2011 12:50'],
dtype='object', name='InvoiceDate', length=541909)
In [56]:
# change the names of the DataFrmae
sales_df.index.set_names(names = ['Transaction Location','Transaction Date'], inplace=True)
sales_df.head()
Out[56]:
InvoiceNo | StockCode | Description | Quantity | UnitPrice | CustomerID | ||
---|---|---|---|---|---|---|---|
Transaction Location | Transaction Date | ||||||
United Kingdom | 12/1/2010 8:26 | 536365 | 85123A | WHITE HANGING HEART T-LIGHT HOLDER | 6 | 2.55 | 17850.0 |
12/1/2010 8:26 | 536365 | 71053 | WHITE METAL LANTERN | 6 | 3.39 | 17850.0 | |
12/1/2010 8:26 | 536365 | 84406B | CREAM CUPID HEARTS COAT HANGER | 8 | 2.75 | 17850.0 | |
12/1/2010 8:26 | 536365 | 84029G | KNITTED UNION FLAG HOT WATER BOTTLE | 6 | 3.39 | 17850.0 | |
12/1/2010 8:26 | 536365 | 84029E | RED WOOLLY HOTTIE WHITE HEART. | 6 | 3.39 | 17850.0 |
728x90
'Data Analytics with python > [Data Analysis]' 카테고리의 다른 글
[matplotlib]S5_01_single_line_plot (0) | 2023.01.21 |
---|---|
[Pandas][DataFrame][MultiIndex]S4_05_Multi_indexing_operations2 (0) | 2023.01.21 |
[Pandas][DataFrame][MultiIndex]S4_03_Multi_index_dataframe (0) | 2023.01.21 |
[Pandas][DataFrame][MultiIndex]S4_02_grouping (0) | 2023.01.21 |
[Pandas][DataFrame][MultiIndex]S4_01_Explore_dataset (0) | 2023.01.21 |
댓글