Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

데이터분석 공부하기

[날짜] 날짜데이터 Transform하는 방법 본문

Python

[날짜] 날짜데이터 Transform하는 방법

Eileen's 2021. 10. 19. 15:38

1. Datetime 활용:

oil_18['기간'] = oil_18['기간'].apply(lambda x: pd.to_datetime(str(int(x))))
def date_split(df):
  df['year'] = df['datetime'].dt.year
  df['month'] = df['datetime'].dt.month
  df['day'] = df['datetime'].dt.day
  df['week'] = df['datetime'].dt.week

 

2. 함수 활용(def, apply):

def date_year(s):
return int(s[:4])

def date_month(s):
return int(s[5:7])

def date_day(s):
return int(s[8:-1])

car_recall['recall_year'] = car_recall['리콜개시일'].apply(date_year)
car_recall['recall_month'] = car_recall['리콜개시일'].apply(date_month)
car_recall['recall_day'] = car_recall['리콜개시일'].apply(date_day)

 

 

3. for문 활용:
-기존 데이터 (ex- 2020106)

year = []
month = []

for i in metro_drop['사용월']:
  year.append(i[:4])
  month.append(i[-2:])


metro_drop['year'] = year
metro_drop['month'] = month

metro_june = metro_drop[metro_drop['month'] == '06']