아미(아름다운미소)

pandas 차주 월요일데이타 본문

랭귀지/pandas

pandas 차주 월요일데이타

유키공 2024. 7. 11. 08:22
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'year': [2024],
    'week': [5]
})

# 다음 주 월요일 00시 계산
df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w') + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.values[0])
df['next_monday'] = df['next_monday'].dt.floor('d')  # 시간을 0시로 설정

# 날짜 형식 변경
df['next_monday'] = df['next_monday'].dt.strftime('%m/%d/%Y %I:%M:%S %p')

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': [20520101]
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'] // 10000
df['week'] = (df['PNdas_date'] % 10000) // 100

# 마지막 주차인 경우 다음 해로 넘어가는 처리
df.loc[df['week'] == 1, 'year'] = df['year'] + 1

df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w') + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.values[0])
df['next_monday'] = df['next_monday'].dt.floor('d')  # 시간을 0시로 설정

# 날짜 형식 변경
df['next_monday'] = df['next_monday'].dt.strftime('%m/%d/%Y %I:%M:%S %p')

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['205201']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)

# 마지막 주차인 경우 다음 해로 넘어가는 처리
df.loc[df['week'] == 1, 'year'] = df['year'] + 1

df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w') + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.values[0])
df['next_monday'] = df['next_monday'].dt.floor('d')  # 시간을 0시로 설정

# 날짜 형식 변경
df['next_monday'] = df['next_monday'].dt.strftime('%m/%d/%Y %I:%M:%S %p')

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['205201']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)

# 마지막 주차인 경우 다음 해로 넘어가는 처리
df.loc[df['week'] == 1, 'year'] = df['year'] + 1

df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w') + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.values[0])
df['next_monday'] = df['next_monday'].dt.floor('d')  # 시간을 0시로 설정

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['205201']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)

# 마지막 주차인 경우 다음 해로 넘어가는 처리
df.loc[df['week'] == 1, 'year'] = df['year'] + 1

df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = df['current_date'] + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.values[0])

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['202432']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)
df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = df['current_date'] + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.values[0])

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['202432']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)
df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = df['current_date'] + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday)

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['202432']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)
df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = df['current_date'] + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.astype(float)) + pd.Timedelta(hours=0)

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['202426', '202427', '202428', '202429']
})

# PNdas 주간 기준 현재 날짜 계산 함수
def get_next_monday(pndas_date_str):
    year = int(pndas_date_str[:4])
    week = int(pndas_date_str[4:])
    current_date = pd.Timestamp(f"{year}-W{week}-1")
    next_monday = current_date + pd.Timedelta(days=7) - pd.Timedelta(days=current_date.weekday()) + pd.Timedelta(hours=0)
    return next_monday

# 데이터프레임 열에 함수 적용
df['next_monday'] = df['PNdas_date'].apply(get_next_monday)

print(df)
import pandas as pd

# 샘플 데이터프레임 생성
df = pd.DataFrame({
    'PNdas_date': ['202426', '202427', '202428', '202429']
})

# PNdas 주간 기준 현재 날짜와 다음 주 월요일 00시 계산
df['year'] = df['PNdas_date'].str[:4].astype(int)
df['week'] = df['PNdas_date'].str[4:].astype(int)
df['current_date'] = pd.to_datetime(df['year'].astype(str) + '-W' + df['week'].astype(str) + '-1', format='%Y-W%W-%w')
df['next_monday'] = df['current_date'] + pd.Timedelta(days=7) - pd.Timedelta(days=df['current_date'].dt.weekday.apply(float)) + pd.Timedelta(hours=0)

print(df)

'랭귀지 > pandas' 카테고리의 다른 글

pandas groupby sum  (0) 2024.07.12
pandas index 확인  (0) 2024.07.11
pandas 주차구하기  (0) 2024.07.09
pandas 값이 존재할때만 split  (0) 2024.07.08
pandas 에서 aaa.str[:4] a가없을경우 제외하기  (0) 2024.07.08
Comments