Programming Languages/Python

[python/pandas] read_excel, header, stack, reset_index

헐리 2021. 7. 6. 10:38

read_excel

data_frame: DataFrame = pd.read_excel("C:\\example\\ex.xlsx")

excel 파일을 읽어오기

 

header

data_frame: DataFrame = pd.read_excel("C:\\example\\ex.xlsx", header=[0,1,2,3])
음식 떡볶이
샐러드
불고기
파스타

컬럼이 (떡볶이, 샐러, 불고기, 파스타)가 됨

 

stack

body_dataframe : DataFrame = body_dataframe.stack(level=[0,1,2])
>>> df_multi_level_cols1
     weight
         kg    pounds
cat       1        2
dog       2        4
>>> df_multi_level_cols1.stack()
            weight
cat kg           1
    pounds       2
dog kg           2
    pounds       4

 

reset_index

body_dataframe.reset_index(inplace=True)
>>> df
         class  max_speed
falcon    bird      389.0
parrot    bird       24.0
lion    mammal       80.5
monkey  mammal        NaN
>>> df.reset_index()
    index   class  max_speed
0  falcon    bird      389.0
1  parrot    bird       24.0
2    lion  mammal       80.5
3  monkey  mammal        NaN

 

inplace = False: 명령어를 실행 한 후 메소드가 적용된 데이터 프레임을 기존 데이터 프레임으로 대체

 

inplace = True:  명령어를 실행 한 후 메소드가 적용된 데이터 프레임으로 반환

즉, 삭제 메소드를 실행했다면 반환값은 컬럼이 삭제된 Dataframe이 됨