datetime
1. strptime : 문자열 -> 날짜(datetime 객체)
from datetime import datetime
date_string = "2025-03-18 14:30:00"
time_data = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print(time_data) # 2025-03-18 14:30:00
print(type(time_data)) # <class 'datetime.datetime'>
2. strftime : 날짜(datetime객체) -> 문자열
time_data.strftime('%y/%m/%d')
time_data.strftime('%m월')
time_data.strftime('%w')
time_data.strftime('%a')
Timestamp in Pandas
Python의 datetime과 유사하지만, 더 정밀한 시간 단위(나노초 단위 지원) 와 강력한 연산 기능을 제공
ts = pd.Timestamp("2025-03-18 14:30:00")
ts.year
ts.month
# 특정 날짜에서 날짜 연산 가능
new_ts = ts + pd.Timedelta(days=5, hours=3)
# timestamp -> 문자열
date_str = ts.strftime("%Y/%m/%d %H:%M:%S")
# 문자열 -> timestamp
ts_from_str = pd.to_datetime("2025-03-18 14:30:00")
time_data2 = pd.to_datetime('2025년 03월 07일', format="%Y년 %m월 %d일")
time_data2 - pd.Timedelta('3 days')
time_data2 - pd.Timedelta(days= 30)
데이터프레임간의 결합
concat()
그냥 데이터를 위/아래/옆으로 이어붙이기
axis : 0(행), 1(열)
concat_df = pd.concat([df1, df2], axis=0, ignore_index = True)
merge()
공통된 열을 기준으로 데이터를 합치기
how -> inner, left, right, outer
merged_df = pd.merge(df1, df2, on='id', how ='inner')
기준이 되는 컬럼의 이름이 다른 경우
pd.merge(df4, df5, left_on='loc', right_on='location', how='inner')
# 두개의 데이터프레임 중 하나의 이름을 변경하고 결합
df5.rename(columns = {'location' : 'loc'}, inplace=True)
pd.merge(df4, df5, on='loc', how='inner')
duplicated()
중복 데이터를 체크하는 함수
subset 매개변수 : 어떤 열의 데이터의 중복을 체크할 것인지
keep : first, last, False
flag = total_df.duplicated(subset='transaction_id', keep=False)
total_df.loc[flag]
중복 데이터를 제거할때는 : df.drop_duplicates(subset = '', keep= )
'Data Analysis > Python for DA' 카테고리의 다른 글
| [Invest Class] 1. buyandhold 함수 생성 (1) | 2025.03.28 |
|---|---|
| [pymysql] SQL과 Python 연결 (1) | 2025.03.19 |
| [데이터 수집] Requests/ BeautifulSoup (1) | 2025.03.14 |
| [pandas] 데이터 분석 복습 2 (0) | 2025.03.14 |
| [pandas] 데이터 분석 복습 (0) | 2025.03.10 |