Python_Intermediate/Pandas
Pandas - Gapminder Data 분석(그래프 분석) 3
AnKiWoong
2019. 5. 20. 10:18
반응형
1 Sample Data
2. import module
import pandas as pd
import matplotlib.pyplot as plt
3. 그래프 분석
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data\gapminder.tsv', sep='\t')
year_lifeExp_mean = df.groupby('year')['lifeExp'].mean()
year = df.loc[0: , 'year']
year_drop = year.drop_duplicates()
year_x = []
for i in year_drop:
year_x.append(i)
plt.rcParams["font.family"] = 'NanumGothic'
plt.rcParams["font.size"] = 10
plt.rcParams["figure.figsize"] = (14, 10)
plt.figure()
year_lifeExp_mean.plot(linestyle='--', marker='.')
plt.grid()
plt.title("연도별 기대 수명의 평균값")
plt.ylabel("나이")
plt.xlabel('년도')
plt.xticks(year_x)
plt.savefig('life.png')
plt.close()
4. Code 풀이
- 연도별 기대 수명 평균 값(그룹화 작업)
year_lifeExp_mean = df.groupby('year')['lifeExp'].mean()
print(year_lifeExp_mean)
year
1952 49.057620
1957 51.507401
1962 53.609249
1967 55.678290
1972 57.647386
1977 59.570157
1982 61.533197
1987 63.212613
1992 64.160338
1997 65.014676
2002 65.694923
2007 67.007423
Name: lifeExp, dtype: float64
Process finished with exit code 0
- plt x 축을 위한 데이터 구성(year 중복값 제거 포함)
year = df.loc[0: , 'year']
year_drop = year.drop_duplicates()
print(year_drop)
0 1952
1 1957
2 1962
3 1967
4 1972
5 1977
6 1982
7 1987
8 1992
9 1997
10 2002
11 2007
Name: year, dtype: int64
Process finished with exit code 0
- 리스트 화 하여 x 축 구성
year_x = []
for i in year_drop:
year_x.append(i)
print(year_x)
[1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007]
Process finished with exit code 0
- 선 그래프 표현 작업
plt.rcParams["font.family"] = 'NanumGothic'
plt.rcParams["font.size"] = 10
plt.rcParams["figure.figsize"] = (14, 10)
plt.figure()
year_lifeExp_mean.plot(linestyle='--', marker='.')
plt.grid()
plt.title("연도별 기대 수명의 평균값")
plt.ylabel("나이")
plt.xlabel('년도')
plt.xticks(year_x)
plt.savefig('life.png')
plt.close()
반응형