티스토리 뷰

파이썬[python]

파이썬 강좌 5

xemaker 2022. 5. 13. 17:05
[1]
import numpy as np
import pandas as pd
[2]
df = pd.read_csv("https://raw.githubusercontent.com/SoongMoo/soldesk2110/main/data/scientists.csv")
df

[11]
print(df['Age'])
print(df['Age'].mean())
#### 평균나이보다 많은 사람을 출력하시오.
# 마스킹
df[df['Age'] > df['Age'].mean()]
# 마스크
df['Age'] > df['Age'].mean()
df[[False,True,True,True,False,False,False,True]]
df['Age'][df['Age'] > df['Age'].mean()]
0    37
1    61
2    90
3    66
4    56
5    45
6    41
7    77
Name: Age, dtype: int64
59.125

1    61
2    90
3    66
7    77
Name: Age, dtype: int64
[24]
df
#df.info(df['Died'] - df['Born'])
Born = pd.to_datetime(df['Born'], format='%Y-%m-%d')# 1999-12-10
# pd.to_datetime(19991210, format='%Y%m%d')
Died = pd.to_datetime(df['Died'], format='%Y-%m-%d')
df['Born_dt'] , df['Died_dt'] = Born, Died
df
df.info()
df['age_days_dt'] = (Died - Born) / 365
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 7 columns):
 #   Column      Non-Null Count  Dtype         
---  ------      --------------  -----         
 0   Name        8 non-null      object        
 1   Born        8 non-null      object        
 2   Died        8 non-null      object        
 3   Age         8 non-null      int64         
 4   Occupation  8 non-null      object        
 5   Born_dt     8 non-null      datetime64[ns]
 6   Died_dt     8 non-null      datetime64[ns]
dtypes: datetime64[ns](2), int64(1), object(4)
memory usage: 576.0+ bytes

[27]
### DataFrame에 있는 데이터를 파일에 저장
df.to_csv("df.csv")
df.to_csv("df.tsv" , sep="\t")
df.to_csv("df1.csv", index=False)
[28]
import xlwt
df.to_excel('df.xls')
C:\Users\SCOOL\AppData\Local\Temp\ipykernel_9532\2880873007.py:2: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  df.to_excel('df.xls')

[30]
import openpyxl
df.to_excel('df.xlsx')
[31]
df.to_excel('df1.xlsx',index=False)
[33]
df = df.drop(['Born_dt', 'Died_dt', 'age_days_dt'], axis = 1)
df

[34]
df = df.drop([1,3,5], axis = 0)
df

# pandas 의 시계열
# 타임스템프 (timestamp : 1970년도 1월1일부터 현재까지의 시간을 ms로 계산,325233353)
# 시간 간격, 기간 ( period )
# 시간 델타, 지속기간( duration )
[35]
from datetime import datetime
datetime(year=2015, month=7, day=4)
datetime.datetime(2015, 7, 4, 0, 0)
[36]
from dateutil import parser
date = parser.parse("4th of July, 2015")
date
datetime.datetime(2015, 7, 4, 0, 0)
[42]
date.strftime('%W')
'26'
[43]
# numpy
date = np.array('2015-07-04', dtype=np.datetime64)
date
array('2015-07-04', dtype='datetime64[D]')
[44]
date + np.arange(12)
array(['2015-07-04', '2015-07-05', '2015-07-06', '2015-07-07',
       '2015-07-08', '2015-07-09', '2015-07-10', '2015-07-11',
       '2015-07-12', '2015-07-13', '2015-07-14', '2015-07-15'],
      dtype='datetime64[D]')
[45]
np.datetime64('2015-07-04')
numpy.datetime64('2015-07-04')
[46]
np.datetime64('2015-07-04 12:00')
numpy.datetime64('2015-07-04T12:00')
[47]
np.datetime64('2015-07-04 12:59:59.50', 'ns')
numpy.datetime64('2015-07-04T12:59:59.500000000')
[48]
# pandas
date = pd.to_datetime("4th of July, 2015")
[51]
date.strftime("%W")
'26'
[52]
date + pd.to_timedelta(np.arange(12), 'D')
DatetimeIndex(['2015-07-04', '2015-07-05', '2015-07-06', '2015-07-07',
               '2015-07-08', '2015-07-09', '2015-07-10', '2015-07-11',
               '2015-07-12', '2015-07-13', '2015-07-14', '2015-07-15'],
              dtype='datetime64[ns]', freq=None)
[63]
#시간으로 인덱싱하기
index = pd.DatetimeIndex(['2014-07-04', '2014-08-04',
                          '2015-07-04', '2015-08-04'])
data =  pd.Series([0,1,2,3], index = index)
data
data['2014-07-04']
data['2014-07-04':'2015-07-04']
data['2015']
data['2015-07']
2015-07-04    2
dtype: int64
[64]
dates = pd.to_datetime([datetime(2015, 7, 3), '4th of July, 2015',
                       '2015-Jul-6', '07-07-2015', '20150708'])
dates
DatetimeIndex(['2015-07-03', '2015-07-04', '2015-07-06', '2015-07-07',
               '2015-07-08'],
              dtype='datetime64[ns]', freq=None)
[69]
dates[1] - dates[0]
dates  -  dates[0]
TimedeltaIndex(['0 days', '1 days', '3 days', '4 days', '5 days'], dtype='timedelta64[ns]', freq=None)
[80]
# date_range
pd.date_range('2015-07-03',periods = 8, freq='D')
DatetimeIndex(['2015-07-03', '2015-07-04', '2015-07-05', '2015-07-06',
               '2015-07-07', '2015-07-08', '2015-07-09', '2015-07-10'],
              dtype='datetime64[ns]', freq='D')
[72]
pd.date_range('2015-07-03',periods = 8, freq='H')
DatetimeIndex(['2015-07-03 00:00:00', '2015-07-03 01:00:00',
               '2015-07-03 02:00:00', '2015-07-03 03:00:00',
               '2015-07-03 04:00:00', '2015-07-03 05:00:00',
               '2015-07-03 06:00:00', '2015-07-03 07:00:00'],
              dtype='datetime64[ns]', freq='H')
[79]
pd.date_range('2015-07-31',periods = 8, freq='M')
DatetimeIndex(['2015-07-31', '2015-08-31', '2015-09-30', '2015-10-31',
               '2015-11-30', '2015-12-31', '2016-01-31', '2016-02-29'],
              dtype='datetime64[ns]', freq='M')
[96]
pd.date_range( 5 ,periods = 8, freq='H')
DatetimeIndex(['1970-01-01 00:00:00.000000005',
               '1970-01-01 01:00:00.000000005',
               '1970-01-01 02:00:00.000000005',
               '1970-01-01 03:00:00.000000005',
               '1970-01-01 04:00:00.000000005',
               '1970-01-01 05:00:00.000000005',
               '1970-01-01 06:00:00.000000005',
               '1970-01-01 07:00:00.000000005'],
              dtype='datetime64[ns]', freq='H')
[121]
pd.timedelta_range(0, periods = 14, freq='D')
pd.timedelta_range(0, periods = 14, freq='H')
pd.timedelta_range("00:00:00", periods = 14, freq='H')
pd.timedelta_range(0, periods = 14, freq='2H30T')
pd.timedelta_range(0, periods = 14, freq='3D')
TimedeltaIndex([ '0 days',  '3 days',  '6 days',  '9 days', '12 days',
                '15 days', '18 days', '21 days', '24 days', '27 days',
                '30 days', '33 days', '36 days', '39 days'],
               dtype='timedelta64[ns]', freq='3D')
[123]
pd.date_range('2022-05-13',  periods=5 , freq='D')
DatetimeIndex(['2022-05-13', '2022-05-14', '2022-05-15', '2022-05-16',
               '2022-05-17'],
              dtype='datetime64[ns]', freq='D')
[124]
from pandas.tseries.offsets import BDay
pd.date_range('2022-05-13',  periods=5 , freq=BDay())
DatetimeIndex(['2022-05-13', '2022-05-16', '2022-05-17', '2022-05-18',
               '2022-05-19'],
              dtype='datetime64[ns]', freq='B')
[165]
from pandas_datareader import data
yahoo = data.DataReader('GOOG', start='2004', end='2016',
                       data_source='yahoo')
yahoo = yahoo['Close']
# yahoo종가 
# yahoo['Close']
[6]
import matplotlib.pyplot as plt
[149]
yahoo.plot(alpha=0.5, style='-')
yahoo.resample('BA').mean().plot(style=":")
yahoo.asfreq('BA').plot(style="--")
plt.legend(['input', 'resample', 'asfreq'], loc = 'upper left')
<matplotlib.legend.Legend at 0x1e17ebe3550>

[49]
yahoo.resample('BA').mean()
yahoo.asfreq('BA')
Date
2004-12-31     96.035034
2005-12-30    206.655411
2006-12-29    229.380234
2007-12-31    344.448914
2008-12-31    153.250580
2009-12-31    308.832428
2010-12-31    295.875977
2011-12-30    321.744019
2012-12-31    352.369232
2013-12-31    558.262512
2014-12-31    524.958740
2015-12-31    758.880005
Freq: BA-DEC, Name: Close, dtype: float64
[150]
fig, ax = plt.subplots(2, sharex=True)
data = yahoo.iloc[:10]
data.asfreq('D').plot(ax=ax[0] ,marker='o')
data.asfreq('D', method='bfill' ).plot(ax=ax[1], style='-o')
data.asfreq('D', method='ffill' ).plot(ax=ax[1], style='--o')
<AxesSubplot:xlabel='Date'>

[170]
data1 = yahoo.asfreq('D').iloc[:10]
data1
#print(data1.fillna(method='ffill'))
print(data1.fillna(method='pad'))
Date
2004-08-19    49.982655
2004-08-20    53.952770
2004-08-21    53.952770
2004-08-22    53.952770
2004-08-23    54.495735
2004-08-24    52.239197
2004-08-25    52.802086
2004-08-26    53.753517
2004-08-27    52.876804
2004-08-28    52.876804
Freq: D, Name: Close, dtype: float64

[174]
print(yahoo.asfreq('D').head())
print(yahoo.asfreq('D', method='pad'))
print(yahoo.asfreq('D', method='ffill'))
Date
2004-08-19    49.982655
2004-08-20    53.952770
2004-08-21          NaN
2004-08-22          NaN
2004-08-23    54.495735
Freq: D, Name: Close, dtype: float64
Date
2004-08-19     49.982655
2004-08-20     53.952770
2004-08-21     53.952770
2004-08-22     53.952770
2004-08-23     54.495735
                 ...    
2015-12-27    748.400024
2015-12-28    762.510010
2015-12-29    776.599976
2015-12-30    771.000000
2015-12-31    758.880005
Freq: D, Name: Close, Length: 4152, dtype: float64
Date
2004-08-19     49.982655
2004-08-20     53.952770
2004-08-21     53.952770
2004-08-22     53.952770
2004-08-23     54.495735
                 ...    
2015-12-27    748.400024
2015-12-28    762.510010
2015-12-29    776.599976
2015-12-30    771.000000
2015-12-31    758.880005
Freq: D, Name: Close, Length: 4152, dtype: float64

[182]
# shift(), tshift()
yahoo.iloc[0:20]
data2 = yahoo.shift(10).iloc[0:20]
data2
Date
2004-08-19          NaN
2004-08-20          NaN
2004-08-23          NaN
2004-08-24          NaN
2004-08-25          NaN
2004-08-26          NaN
2004-08-27          NaN
2004-08-30          NaN
2004-08-31          NaN
2004-09-01          NaN
2004-09-02    49.982655
2004-09-03    53.952770
2004-09-07    54.495735
2004-09-08    52.239197
2004-09-09    52.802086
2004-09-10    53.753517
2004-09-13    52.876804
2004-09-14    50.814533
2004-09-15    50.993862
2004-09-16    49.937820
Name: Close, dtype: float64
[196]
fig, ax = plt.subplots(3, sharey=True)
yahoo = yahoo.asfreq('D', method='pad')

yahoo.plot(ax=ax[0])
yahoo.shift(900).plot(ax=ax[1])
yahoo.tshift(900).plot(ax=ax[2])

local_max = pd.to_datetime('2007-11-05')
offset = pd.Timedelta(900,'D')

ax[0].legend(['input'], loc = 2)
ax[0].get_xticklabels()[2].set(weight='heavy', color='red')
ax[0].axvline(local_max, alpha=0.3, color='red')

ax[1].axvline(local_max + offset, alpha=0.3, color='red')
ax[2].axvline(local_max + offset, alpha=0.3, color='red')
C:\Users\SCOOL\AppData\Local\Temp\ipykernel_13972\1261222075.py:6: FutureWarning: tshift is deprecated and will be removed in a future version. Please use shift instead.
  yahoo.tshift(900).plot(ax=ax[2])

<matplotlib.lines.Line2D at 0x1e17ff6b580>

[203]
rolling = yahoo.rolling(365, center=True)
data = pd.DataFrame({'input':yahoo, 
                    'rolling_mean' : rolling.mean(),
                    'rolling_std' : rolling.std()})
ax = data.plot(style=['-', '--', ':'])

[206]
data = pd.read_csv("Fremont_Bridge_Bicycle_Counter.csv",
                   index_col='Date' , parse_dates=True)
data


[208]
data.columns = ['West', 'East', 'Total']
data

[209]
data['TTotal'] = data['West'] + data['East']
data

[215]
data = data.drop(['Total'], axis=1)
[217]
data.dropna().describe()

[218]
data.plot()
<AxesSubplot:xlabel='Date'>

[221]
weekly = data.resample('W').sum()
weekly.plot(style=[':', '--', '-'])
<AxesSubplot:xlabel='Date'>

[233]
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('classic')
x = np.linspace(0, 10, 100)
x
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x) , '--')
[<matplotlib.lines.Line2D at 0x1e10b24d480>]

[234]
fig.savefig('my_figure.png')
[235]
from IPython.display import Image
Image('my_figure.png')

[239]
x = np.linspace(0, 10, 30)
y = np.sin(x)
plt.plot(x, y, 'o' ,color='black')
[<matplotlib.lines.Line2D at 0x1e10c0fe650>]

[243]
plt.plot(x, y, '-ok');

[245]
plt.plot(x, y, marker='o');

[251]
rng = np.random.RandomState(0)
x = rng.randn(100)
y = rng.randn(100)
plt.scatter(x, y, c = "red" , s = 100, alpha = 0.3)
<matplotlib.collections.PathCollection at 0x1e111a4edd0>

[260]
from sklearn.datasets import load_iris
iris = load_iris()
iris
features = iris.data.T
features
plt.scatter(features[0], features[1], s = 100 * features[3] , c= iris.target)
<matplotlib.collections.PathCollection at 0x1e113104dc0>

[270]
data = np.random.randn(1000)
plt.hist(data, bins=40, histtype='stepfilled', edgecolor='none' ,color='red') # 분포도
(array([ 1.,  0.,  1.,  2.,  4.,  1., 10.,  8., 10., 12., 11., 27., 32.,
        34., 48., 55., 44., 65., 67., 76., 54., 74., 56., 55., 50., 45.,
        36., 23., 21., 22., 15., 15., 11.,  3.,  5.,  3.,  1.,  1.,  1.,
         1.]),
 array([-3.28091133, -3.119287  , -2.95766267, -2.79603834, -2.63441401,
        -2.47278968, -2.31116535, -2.14954102, -1.98791669, -1.82629236,
        -1.66466803, -1.5030437 , -1.34141937, -1.17979504, -1.01817072,
        -0.85654639, -0.69492206, -0.53329773, -0.3716734 , -0.21004907,
        -0.04842474,  0.11319959,  0.27482392,  0.43644825,  0.59807258,
         0.75969691,  0.92132124,  1.08294557,  1.2445699 ,  1.40619423,
         1.56781856,  1.72944289,  1.89106722,  2.05269155,  2.21431588,
         2.37594021,  2.53756454,  2.69918887,  2.8608132 ,  3.02243753,
         3.18406186]),
 [<matplotlib.patches.Polygon at 0x1e11331a1d0>])

[274]
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype="stepfilled", alpha=0.3, bins=40 )
plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs)
(array([ 1.,  0.,  0.,  3.,  1.,  4.,  3.,  2.,  5.,  7., 16., 15., 22.,
        20., 26., 32., 51., 47., 63., 61., 57., 59., 55., 75., 52., 63.,
        42., 41., 31., 41., 27., 25.,  9., 14.,  7., 11.,  5.,  1.,  4.,
         2.]),
 array([-4.22827969, -3.90389966, -3.57951962, -3.25513959, -2.93075955,
        -2.60637952, -2.28199949, -1.95761945, -1.63323942, -1.30885938,
        -0.98447935, -0.66009932, -0.33571928, -0.01133925,  0.31304079,
         0.63742082,  0.96180085,  1.28618089,  1.61056092,  1.93494095,
         2.25932099,  2.58370102,  2.90808106,  3.23246109,  3.55684112,
         3.88122116,  4.20560119,  4.52998123,  4.85436126,  5.17874129,
         5.50312133,  5.82750136,  6.1518814 ,  6.47626143,  6.80064146,
         7.1250215 ,  7.44940153,  7.77378156,  8.0981616 ,  8.42254163,
         8.74692167]),
 [<matplotlib.patches.Polygon at 0x1e114ea8b80>])

[283]
#hist2d
mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
plt.hist2d(x1,x2, bins=30,cmap='Blues' );
cb = plt.colorbar()
cb.set_label('counts in bin')

[284]
plt.hist2d(x1,x2, bins=30,cmap='Blues' );
cb = plt.colorbar(label='count in bin')

[292]
# 범주
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), '-b', label = 'sine')
ax.plot(x, np.cos(x), '--r', label = 'Cosine')
# ax.legend(loc='upper left', frameon=False)
ax.legend(loc='lower center', frameon=False, ncol= 2)
<matplotlib.legend.Legend at 0x1e117c125f0>

[295]
import seaborn as sns
iris = sns.load_dataset("iris")
iris.head()

[296]
sns.pairplot(iris, hue='species' , size=2.5)
C:\Users\SCOOL\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\axisgrid.py:2076: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
  warnings.warn(msg, UserWarning)

<seaborn.axisgrid.PairGrid at 0x1e117a74400>

[297]
tips = sns.load_dataset('tips')
tips.head()

[305]
tips['tip_pct'] = 100 * tips['tip'] / tips['total_bill']
tips
grid = sns.FacetGrid(tips, row="sex" , col="time", margin_titles=True )
grid.map(plt.hist, 'tip_pct',bins=np.linspace(0, 40, 15));

import urllib.parse
API = "http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp"
# 매개변수를 URL 인코딩합니다. --- (※1)
values = {
    'stnId': '108'
}
params = urllib.parse.urlencode(values)
# 요청 전용 URL을 생성합니다. --- (※2)
url = API + "?" + params
print("url=", url)
# 다운로드합니다. --- (※3)
data = urllib.request.urlopen(url).read()
text = data.decode("utf-8")
print(text)
# BeautifulSoup 기본 사용법
from bs4 import BeautifulSoup
# 분석하고 싶은 HTML --- (※2)
html = """
<html><body>
  <h1>스크레이핑이란?</h1>
  <p>웹 페이지를 분석하는 것</p>
  <p>원하는 부분을 추출하는 것</p>
</body></html>
"""
# HTML 분석하기 --- (※3)
soup = BeautifulSoup(html, 'html.parser')
# 원하는 부분 추출하기 --- (※4)
h1 = soup.html.body.h1
p1 = soup.html.body.p
p2 = p1.next_sibling.next_sibling
# 요소의 글자 출력하기 --- (※5)
print("h1 = " + h1.string)
print("p  = " + p1.string)
print("p  = " + p2.string)
html = """
<html><body>
  <h1 id="title">스크레이핑이란?</h1>
  <p id="body">웹 페이지를 분석하는 것</p>
  <p>원하는 부분을 추출하는 것</p>
</body></html>
"""

# HTML 분석하기 --- (※1)
soup = BeautifulSoup(html, 'html.parser')

# find() 메서드로 원하는 부분 추출하기 --- (※2)
title = soup.find(id="title")
body = soup.find(id="body")

# 텍스트 부분 출력하기
print("#title=" + title.string)
print("#body=" + body.string)
soup = BeautifulSoup("<p><a href='a.html'>test</a></p> ", "html.parser")

#분석이 제대로 되었는지 확인 --- (※1)
print(soup.prettify())  # pretify메서드를 이용하면 제대로 분석되었는지 확인

# <a>태그를 변수 a에 할당
a = soup.p.a

# attrs속성의 자료형 확인  --- (※2)
print(a)
print(a.attrs)
print(type(a.attrs))

# href 속성이 있는지 확인
print ("href" in a.attrs)

#href속성값 확인
print(a['href']
url = "http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp"
# urlopen()으로 데이터 가져오기 --- (※1)
res = req.urlopen(url)
# BeautifulSoup으로 분석하기 --- (※2)
soup = BeautifulSoup(res, "html.parser")
# 원하는 데이터 추출하기 --- (※3)
title = soup.find("title").string
wf = soup.find("wf").string
print(title)
print(wf)
### CSS 선택자 사용하기
# 분석 대상 HTML --- (※1)
html = """
<html><body>
<div id="meigen">
  <h1>위키북스 도서</h1>
  <ul class="items">
    <li>유니티 게임 이펙트 입문</li>
    <li>스위프트로 시작하는 아이폰 앱 개발 교과서</li>
    <li>모던 웹사이트 디자인의 정석</li>
  </ul>
</div>
</body></html>
"""
# HTML 분석하기 --- (※2)
soup = BeautifulSoup(html, 'html.parser')
# 필요한 부분을 CSS 쿼리로 추출하기
# 타이틀 부분 추출하기 --- (※3)
h1 = soup.select_one("div#meigen > h1").string
print("h1 =", h1)
# 목록 부분 추출하기 --- (※4)
li_list = soup.select("div#meigen > ul.items > li") 
print(li_list) # selecr는 리스트로 가져온다.
for li in li_list:
  print("li =", li.string)
# HTML 가져오기
url = "http://finance.naver.com/marketindex/"
res = req.urlopen(url)
# HTML 분석하기
soup = BeautifulSoup(res, "html.parser")
# 원하는 데이터 추출하기 --- (※1)
price = soup.select_one("div.head_info > span.value").string
print("usd/krw =", price)

prices = soup.select("div.head_info > span.value")
print( prices)
for price in prices:
    print(price)
import requests
from bs4 import BeautifulSoup
import pandas
import matplotlib as mpl
import matplotlib.pyplot as plt
 

source = requests.get('https://www.weather.go.kr/weather/observation/currentweather.jsp')
soup = BeautifulSoup(source.content,"html.parser")
 
table = soup.find('table',{'class':'table_develop3'})
data = []
 
print("#"*30)
print("\nHello! Here's today's weather!\n")
print("#"*30)
 
for tr in table.find_all('tr'):
    tds = list(tr.find_all('td'))
    for td in tds:
        if td.find('a'):
            point = td.find('a').text
            temp = tds[5].text
            humidity = tds[9].text
            print("{0:<7} {1:<7} {2:<7}".format(point,temp,humidity))
            data.append([point,temp,humidity])
            
print("#"*30)
print("\nIt ends here. thanks!\n")
print("#"*30)
 
print(data)
 
with open('weather.csv','w', encoding='utf-8') as f:
    f.write('지역, 온도, 습도\n')
    for i in data:
        f.write('{0},{1},{2}\n'.format(i[0],i[1],i[2]))
        
df = pandas.read_csv('weather.csv', index_col='지역' , encoding='utf-8')
 
city_df = df.loc[['서울','인천','대전','대구','광주','부산','울산']]
 
font_name = mpl.font_manager.FontProperties(fname='C:\Windows\Fonts\malgun.ttf').get_name()
mpl.rc('font',family=font_name)
 
ax = city_df.plot(kind='bar',title='날씨',figsize=(12,4),legend=True,fontsize=15)
ax.set_xlabel('도시',fontsize=15)
ax.set_ylabel('기온/습도',fontsize=15)
ax.legend(['기온','습도'],fontsize=15)
 
plt.show()

'파이썬[python]' 카테고리의 다른 글

visual studio code 이클립스 처럼 단축키 만들기  (0) 2022.07.25
파이썬 오프라인 설치  (0) 2022.07.21
파이썬 강좌 4  (0) 2022.05.13
파이썬 강좌 3  (0) 2022.05.12
파이썬 오프라인  (0) 2022.05.11
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함