728x90
반응형
SMALL
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import matplotlib
rc('font', family='AppleGothic')
plt.rcParams['axes.unicode_minus'] = False
data = np.random.randint(1,10, size=4)
data
array([2, 9, 5, 7])
data = np.random.randint(30,101, size=(4,6) )
data
array([[94, 42, 91, 67, 54, 94],
[87, 30, 83, 31, 92, 65],
[83, 43, 99, 43, 37, 59],
[82, 88, 48, 48, 59, 70]])
df = pd.DataFrame( data )
df
df['년도'] = [2018,2018,2019,2019]
df['반'] = ['1반','2반','1반','2반']
df
df.set_index(['년도','반'])
df
df.index = pd.MultiIndex.from_product([[2018,2019],['1반','2반']],
names=['년도','반'])
df
df.index
MultiIndex([(2018, '1반'),
(2018, '2반'),
(2019, '1반'),
(2019, '2반')],
names=['년도', '반'])
df.index.levels[1]
Index(['1반', '2반'], dtype='object', name='반')
df.index.get_level_values(0)
Int64Index([2018, 2018, 2019, 2019], dtype='int64', name='년도')
df.index.get_level_values(1)
Index(['1반', '2반', '1반', '2반'], dtype='object', name='반')
df.drop(columns=['년도','반'],inplace=True)
df
df.reset_index() #인덱스를 칼럼으로 보냄
df
df.loc[2018]
df.columns = pd.MultiIndex.from_product([['홍길동','이순신','임꺽정'],['국어','영어']],)
df
df.columns.levels[0]
Index(['이순신', '임꺽정', '홍길동'], dtype='object')
df.columns.levels[1]
Index(['국어', '영어'], dtype='object')
df['이순신']
review
- pd.MultiIndex.from_product()
728x90
반응형
LIST