지방이의 Data Science Lab

[python] 시간컬럼을 카테고리로 변경 본문

Data Analysis/Python

[python] 시간컬럼을 카테고리로 변경

[지현] 2019. 7. 29. 16:23

컬럼을 이렇게 변경하는 방법

 

 

 

 

 

hrs = trade.time.astype(str).apply(lambda x: x.split(':')[0]).astype(float)

hrs = hrs.to_frame('hrs')

trade= pd.concat([trade,hrs],axis=1)

def get_part_of_day(hour):

    return (

        "morning" if 5 <= hour <= 11

        else

        "afternoon" if 12 <= hour <= 17

        else

        "evening" if 18 <= hour <= 22

        else

        "night"

    )

trade['part_of_day'= [get_part_of_day(x) for x in trade['hrs']]

 

Comments