8-4 선 그래프 – 시간 경과에 따른 데이터 표시
꺾은선형 차트: 데이터를 선으로 나타내는 그래프
시계열 그래프 만들기
economics = pd.read_csv('economics.csv')
economics.head()

sns.lineplot(data = economics, x = 'date', y = 'unemploy')

x축에 연도 표시
1) datetime형 변수 생성
# 날짜 시간 타입 변수 만들기
economics('date2') = pd.to_datetime(economics('date'))
# 변수 타입 확인
economics.info()
"""
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 574 entries, 0 to 573
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 574 non-null object
1 pce 574 non-null float64
2 pop 574 non-null float64
3 psavert 574 non-null float64
4 uempmed 574 non-null float64
5 unemploy 574 non-null int64
6 date2 574 non-null datetime64(ns)
dtypes: datetime64(ns)(1), float64(4), int64(1), object(1)
memory usage: 31.5+ KB
"""
economics(('date', 'date2'))

# 연 추출
economics('date2').dt.year
"""
0 1967
1 1967
2 1967
3 1967
4 1967
...
569 2014
570 2015
571 2015
572 2015
573 2015
Name: date2, Length: 574, dtype: int64
"""
# 월 추출
economics('date2').dt.month
"""
0 7
1 8
2 9
3 10
4 11
..
569 12
570 1
571 2
572 3
573 4
Name: date2, Length: 574, dtype: int64
"""
# 일 추출
economics('date2').dt.day
"""
0 1
1 1
2 1
3 1
4 1
..
569 1
570 1
571 1
572 1
573 1
Name: date2, Length: 574, dtype: int64
"""
2) 연도 변수 생성
# 연도 변수 추가
economics('year') = economics('date2').dt.year
economics.head()

3) x축에 연도 표시
# x축에 연도 표시
sns.lineplot(data = economics, x = 'year', y = 'unemploy')

# 신뢰구간 제거 (선의 위아래에 표시된 면적)
sns.lineplot(data = economics, x = 'year', y = 'unemploy', ci = None)

Do It Yourself – 경제 데이터를 사용하여 분석 문제 해결
Q1 시간이 지남에 따라 psavert(개인 저축률)가 어떻게 변했는지 알고 싶습니다. 수년간 개인 저축률의 변화를 보여주는 시계열 그래프를 생성합니다.
economics('date2') = pd.to_datetime(economics('date'))
economics('year') = economics('date2').dt.year
sns.lineplot(data = economics, x= 'year', y = 'psavert', ci =None)

Q2 2014년 월별 psavert 변화를 보여주는 시계열 그래프를 생성합니다.
economics('month') = economics('date2').dt.month
df_2014 = economics.query('year == 2014')
sns.lineplot(data = df_2014, x ='month', y='psavert', ci =None)

※ 내용
![[python] What will [python] What will](https://cafe.tagproduction.kr/wp-content/plugins/contextual-related-posts/default.png)
