728x90
In [1]:
import pandas as pd
In [2]:
raw_data = {'Bank Client ID': ['1', '2', '3', '4', '5'],
'First Name': ['Robert', 'Benedict', 'Mark', 'Tom', 'Ryan'],
'Last Name': ['Downey', 'Cumberbatch', 'Ruffalo', 'Holland', 'Reynolds']}
bank1_df = pd.DataFrame(raw_data, columns = ['Bank Client ID', 'First Name', 'Last Name'])
In [3]:
raw_data = {'Bank Client ID': ['6', '7', '8', '9', '10'],
'First Name': ['Keanu', 'Vin', 'Chris', 'Tom', 'Dave'],
'Last Name': ['Reeves', 'Diesel', 'Pratt', 'Cruise', 'Batista']}
bank2_df = pd.DataFrame(raw_data, columns = ['Bank Client ID', 'First Name', 'Last Name'])
In [4]:
# use multi-indexing
bank_all_df = pd.concat([bank1_df, bank2_df], keys=['Customers Group 1','Customers Group 2'])
bank_all_df
Out[4]:
Bank Client ID | First Name | Last Name | ||
---|---|---|---|---|
Customers Group 1 | 0 | 1 | Robert | Downey |
1 | 2 | Benedict | Cumberbatch | |
2 | 3 | Mark | Ruffalo | |
3 | 4 | Tom | Holland | |
4 | 5 | Ryan | Reynolds | |
Customers Group 2 | 0 | 6 | Keanu | Reeves |
1 | 7 | Vin | Diesel | |
2 | 8 | Chris | Pratt | |
3 | 9 | Tom | Cruise | |
4 | 10 | Dave | Batista |
In [6]:
# accesss elements
bank_all_df.loc['Customers Group 1', :]
Out[6]:
Bank Client ID | First Name | Last Name | |
---|---|---|---|
0 | 1 | Robert | Downey |
1 | 2 | Benedict | Cumberbatch |
2 | 3 | Mark | Ruffalo |
3 | 4 | Tom | Holland |
4 | 5 | Ryan | Reynolds |
In [7]:
bank_all_df.loc['Customers Group 1', 0]
Out[7]:
Bank Client ID 1
First Name Robert
Last Name Downey
Name: (Customers Group 1, 0), dtype: object
In [14]:
bank_all_df.loc['Customers Group 2','First Name']
Out[14]:
0 Keanu
1 Vin
2 Chris
3 Tom
4 Dave
Name: First Name, dtype: object
In [16]:
bank_all_df.loc['Customers Group 2','First Name'][0]
Out[16]:
'Keanu'
728x90
'Data Analytics with python > [Data Analysis]' 카테고리의 다른 글
[Pandas][DataFrame][MultiIndex]S4_02_grouping (0) | 2023.01.21 |
---|---|
[Pandas][DataFrame][MultiIndex]S4_01_Explore_dataset (0) | 2023.01.21 |
[Pandas][DataFrame][concat]S3_01_concatenation (0) | 2023.01.21 |
[Pandas][DataFrame]S2_14_change_datatypes (0) | 2023.01.21 |
[Pandas][DataFrame]S2_13_FeatureEngineering (0) | 2023.01.21 |
댓글