본문 바로가기

IT To do and To was

22년 1월 12일_python수업 마지막날, team project final, take a resting

728x90
반응형

수요일[라멘먹는데 하미녕이 계속 ...우이씨..]

 

1. 최종 발표 코드 기재

2. wish list기재

 

1. 날짜별 분실물 추이

일별, 월별, 년도별 대중교통 분실물 추이

출처 : 열린데이터 광장

In [1]:
import requests
from bs4 import BeautifulSoup
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import json
import re
import pandas as pd                # 데이터를 저장하고 처리하는 패키지
import matplotlib as mpl           # 그래프를 그리는 패키지
import matplotlib.pyplot as plt    # 그래프를 그리는 패키지
import csv
import matplotlib.dates as mdates
import os

#파일 가지고 오기
df =  pd.read_csv('data/seoulreal.csv',encoding='utf-8')
# d_ = df.sort_values(by='redate', ascending=False, ignore_index=True) #정렬
df.drop(columns=['lostSEQ','state','dateofreceipt','details','place','ID','name','region','company','thing','views','ext'], inplace=True) #사용하지 않는 값 삭제
#isno = None만 찾아주는 것
pd.isna(df)
df = df.dropna() #결측치 제거

day 를 뺀 새로운 열 생성

In [59]:
df['month_date'] = pd.to_datetime(df['redate']).dt.strftime('%Y-%m')
df
Out[59]:
redatekindsmonth_date01234...275516275517275518275519275520
2022-01-06 핸드폰 2022-01
2022-01-06 기타 2022-01
2022-01-04 핸드폰 2022-01
2022-01-04 서류봉투 2022-01
2022-01-06 기타 2022-01
... ... ...
2016-09-21 기타 2016-09
2009-04-22 핸드폰 2009-04
2009-02-07 핸드폰 2009-02
2009-02-05 기타 2009-02
2006-12-04 지갑 2006-12

274950 rows × 3 columns

월별, 일별로 그래프를 만들기 위한 새로운 df생성 및 갯수count (+as_index의 경우 groupby할 때 필요하다고 함

In [60]:
df_new = df.groupby(['month_date','kinds'], as_index=False).count()
df_day = df.groupby(['redate','kinds'], as_index=False).count()
df_day
Out[60]:
redatekindsmonth_date01234...2355223553235542355523556
2006-12-04 지갑 1
2007-11-07 핸드폰 1
2007-11-12 기타 1
2007-11-15 핸드폰 1
2007-11-22 가방 1
... ... ...
2022-01-04 쇼핑백 1
2022-01-04 핸드폰 2
2022-01-06 기타 2
2022-01-06 지갑 1
2022-01-06 핸드폰 2

23557 rows × 3 columns

년도별로 between을 사용하여 새로운 df생성

In [7]:
d21_ = df_new[df_new["month_date"].between("2021-01","2021-12")]
d20_ = df_new[df_new["month_date"].between("2020-01","2020-12")]
d19_ = df_new[df_new["month_date"].between("2019-01","2019-12")]
d18_ = df_new[df_new["month_date"].between("2018-01","2018-12")]
d17_ = df_new[df_new["month_date"].between("2017-01","2017-12")]
d16_ = df_new[df_new["month_date"].between("2016-01","2016-12")]
d15_ = df_new[df_new["month_date"].between("2015-01","2015-12")]
d14_ = df_new[df_new["month_date"].between("2014-01","2014-12")]
d13_ = df_new[df_new["month_date"].between("2013-01","2013-12")]
d12_ = df_new[df_new["month_date"].between("2012-01","2012-12")]
d11_ = df_new[df_new["month_date"].between("2011-01","2011-12")]
d10_ = df_new[df_new["month_date"].between("2010-01","2010-12")]
d09_ = df_new[df_new["month_date"].between("2009-01","2009-12")]
d08_ = df_new[df_new["month_date"].between("2008-01","2008-12")]
d07_ = df_new[df_new["month_date"].between("2007-01","2007-12")]
d06_ = df_new[df_new["month_date"].between("2006-01","2006-12")]
plt.rc('font',family='Malgun Gothic')
In [24]:
plt.figure(figsize=(20,15))
plt.bar(d21_['kinds'], d21_['redate'], color ='r', width = 0.25)
plt.title("2021년도 분실물 현황")
plt.show()
In [15]:
#2021년도

plt.figure(figsize=(27,20))
plt.subplot(4, 4, 1)
plt.bar(d21_['kinds'], d21_['redate'], color ='r', width = 0.25)
plt.title("2021년도 분실물 현황")

#2020년도
plt.subplot(4, 4, 2)
plt.bar(d20_['kinds'], d20_['redate'], color ='r', width = 0.25)
plt.title("2020년도 분실물 현황")

# 2019년도
plt.subplot(4, 4, 3)
plt.bar(d19_['kinds'], d19_['redate'], color ='r', width = 0.25)
plt.title("2019년도 분실물 현황")

# 2018년도

plt.subplot(4, 4, 4)
plt.bar(d18_['kinds'], d18_['redate'], color ='r', width = 0.25)
plt.title("2018년도 분실물 현황")

# 2017년도

plt.subplot(4, 4, 5)
plt.bar(d17_['kinds'], d17_['redate'], color ='r', width = 0.25)
plt.title("2017년도 분실물 현황")

# 2016년도

plt.subplot(4, 4, 6)
plt.bar(d16_['kinds'], d16_['redate'], color ='r', width = 0.25)
plt.title("2016년도 분실물 현황")

# 2015년도

plt.subplot(4, 4, 7)
plt.bar(d15_['kinds'], d15_['redate'], color ='r', width = 0.25)
plt.title("2015년도 분실물 현황")

# 2014년도

plt.subplot(4, 4, 8)
plt.bar(d14_['kinds'], d14_['redate'], color ='r', width = 0.25)
plt.title("2014년도 분실물 현황")

# 2013년도

plt.subplot(4, 4, 9)
plt.bar(d13_['kinds'], d13_['redate'], color ='r', width = 0.25)
plt.title("2013년도 분실물 현황")

# 2012년도

plt.subplot(4, 4, 10)
plt.bar(d12_['kinds'], d12_['redate'], color ='r', width = 0.25)
plt.title("2012년도 분실물 현황")

# 2011년도

plt.subplot(4, 4, 11)
plt.bar(d11_['kinds'], d11_['redate'], color ='r', width = 0.25)
plt.title("2011년도 분실물 현황")

# 2010년도

plt.subplot(4, 4, 12)
plt.bar(d10_['kinds'], d10_['redate'], color ='r', width = 0.25)
plt.title("2010년도 분실물 현황")

# 2009년도

plt.subplot(4, 4, 13)
plt.bar(d09_['kinds'], d09_['redate'], color ='r', width = 0.25)
plt.title("2009년도 분실물 현황")

# 2008년도

plt.subplot(4, 4, 14)
plt.bar(d08_['kinds'], d08_['redate'], color ='r', width = 0.25)
plt.title("2008년도 분실물 현황")

# 2007년도

plt.subplot(4, 4, 15)
plt.bar(d07_['kinds'], d07_['redate'], color ='r', width = 0.25)
plt.title("2007년도 분실물 현황")

# 2006년도

plt.subplot(4, 4, 16)
plt.bar(d06_['kinds'], d06_['redate'], color ='r', width = 0.25)
plt.title("2006년도 분실물 현황")


plt.show()

월별로도 똑같이 생성

In [19]:
m1_ = df_day[df_day["redate"].between("2021-01-01","2021-01-31")]
m2_ = df_day[df_day["redate"].between("2021-02-01","2021-02-31")]
m3_ = df_day[df_day["redate"].between("2021-03-01","2021-03-31")]
m4_ = df_day[df_day["redate"].between("2021-04-01","2021-04-31")]
m5_ = df_day[df_day["redate"].between("2021-05-01","2021-05-31")]
m6_ = df_day[df_day["redate"].between("2021-06-01","2021-06-31")]
m7_ = df_day[df_day["redate"].between("2021-07-01","2021-07-31")]
m8_ = df_day[df_day["redate"].between("2021-08-01","2021-08-31")]
m9_ = df_day[df_day["redate"].between("2021-09-01","2021-09-31")]
m10_ = df_day[df_day["redate"].between("2021-10-01","2021-10-31")]
m11_ = df_day[df_day["redate"].between("2021-11-01","2021-11-31")]
m12_ = df_day[df_day["redate"].between("2021-12-01","2021-12-31")]
In [27]:
# plt.pie(df_day['date'], labels = df_day['kinds'])
plt.title("2021년 7월")
plt.bar(m7_['kinds'], m7_['month_date'])

plt.show()
In [64]:
a = int(input('분실물을 보고싶은 월을 기재하세요.'))


if a > 12 :
    print("1월부터 12월까지 중 숫자만 기입해주세요.")
elif a < 10 :
    month = '0' + str(a)
elif a > 10 :
     month = str(a)
    
start = '2021-'+str(month)+'-01'
end = '2021-'+str(month)+'-31'



df_g = df_day[df_day["redate"].between(start,end)]

plt.title("2021년 %d월"%a)
plt.bar(df_g['kinds'], df_g['month_date'])

plt.show()
 
In [65]:
b =int(input('2006-2021까지 분실물을 보고싶은 년도를 4자리를 기재하세요.'))
if b > 2021 :
    print('2021보다 큰 년도 수는 나타낼 수 없습니다.')
# elif b > 10 :
#     year = '0' + str(b)
elif b < 2006 :
    print('2006보다 작은 년도 수는 나타낼 수 없습니다.')


start = str(b) + "-01"
end = str(b) + "-12"




df_y = df_new[df_new['month_date'].between(start,end)]
df_y
plt.bar(df_y['kinds'], df_y['redate'], width = 0.25)
plt.title('%d년도 분실물 현황'%b)
plt.show()
 
In [29]:
da1_= df_day[df_day["redate"]==("2012-01-10")]
da1_
plt.title("2012-01-10 분실물 현황")
plt.bar(da1_['kinds'],da1_['month_date'])
plt.show()
In [58]:
c =input('2006-2021중 보고싶은 년도의 월을 yyyy-mm-dd 형식으로 기재하세요.')

day_= df_day[df_day["redate"]==("2012-08-09")]
day_
plt.bar(day_['kinds'],day_['month_date'])
plt.grid()
plt.yticks(ticks =range(0, max(day_['month_date'])+1), labels=range(0, max(day_['month_date'])+1))
plt.title('%s 분실물 현황'%c)
plt.show()
 
In [63]:
print("-"*50 +"끝" +"-"*50)
--------------------------------------------------끝--------------------------------------------------

 

tomorrow wish list

. java 활용 조금 알아가기

. 꿀잠자기

728x90
반응형