2020-11-30 python

jsbak·2020년 11월 30일
0

PYTHON

목록 보기
13/24

파이썬 Graph

참고 : https://bcho.tistory.com/1201

from matplotlib import pyplot as plt
import numpy as np

x = np.arange(1,4)
y = x*3
z = x*5

print(x)
print(y)

plt.plot(x,y)
plt.plot(x,z)
plt.show()

삼성, lg 그래프

from matplotlib import pyplot as plt
import pymssql
from openpyxl import load_workbook

lg = []
ss = []

conn = pymssql.connect(server="127.0.0.1", user="sa", password="java", database="mypy")
cursor = conn.cursor()

cursor.execute("select top 10 s_price from stock where s_name='LG'")
row = cursor.fetchone()
while row:
    print(row[0])
    lg.append(row[0])
    row = cursor.fetchone()

cursor.execute("select top 10 s_price from stock where s_name='삼성전자'")
row = cursor.fetchone()
while row:
    print(row[0])
    ss.append(row[0])
    row = cursor.fetchone()

conn.close()

x = range(10)
print(ss)
print(lg)
plt.plot(x,lg, label="lg")
plt.plot(x,ss, label="ss")
plt.legend(loc='upper right')
plt.show()

excel 파일 작성

pandas 참고 : https://ponyozzang.tistory.com/619

import pymssql
import pandas as pd

lg = []
ss = []

conn = pymssql.connect(server="127.0.0.1", user="sa", password="java", database="mypy")
cursor = conn.cursor()

cursor.execute("select top 10 s_price from stock where s_name='LG'")
row = cursor.fetchone()
while row:
    print(row[0])
    lg.append(row[0])
    row = cursor.fetchone()

cursor.execute("select top 10 s_price from stock where s_name='삼성전자'")
row = cursor.fetchone()
while row:
    print(row[0])
    ss.append(row[0])
    row = cursor.fetchone()

conn.close()


raw_data = {'lg' : lg,
             'ss' : ss}
raw_data = pd.DataFrame(raw_data) #데이터 프레임으로 전환
raw_data.to_excel(excel_writer='stock.xlsx', sheet_name='증권', index=False, header=True) #엑셀로 저장

sheet_name='증권', index=False, header=True
시트 이름 , 인덱스 줄지, 헤더이름 줄지 결정

오늘 과제1 엑셀파일을 DB에 넣기

Dataframe 원하는 열 가져오기 iloc
원하는 행 가져오기 loc
열에서 그값 가져오기 loc[0][0]

lociloc

열이름 가져오기


엑셀 파일 -> DB에 저장

import pandas as pd
import pymssql

df = pd.read_excel('stock.xlsx')

conn = pymssql.connect(server="127.0.0.1", user="sa", password="java", database="mypy")
cursor = conn.cursor()
print(df.dtypes)

for i in range(0,2):
    s_name = list(df)[i]
    for j in range(0,10):
        df_iloc = df.iloc[:,[i]]
        s_price  = df_iloc.loc[j][0]
        print(s_name)
        print(s_price)
        
        sql = ""
        sql +="insert into excel (s_name, s_price) values "
        sql += "(%s,%s)"
        cursor.execute(sql,(str(s_name),int(s_price)))
        conn.commit()

conn.close()

데이터 프레임 참고 : https://azanewta.tistory.com/34

오늘 과제2 기보 저장

첫판, 턴,1000~0000, 누가 이겼냐정보?(마지막)
몇판, 몇수, 기보, 이긴정보
SELECT max(pan)+1 from omok
오라클 NVL처럼 그런거 넣어서 만들기
배열에 저장했다가 승부가 났을때 그때 배열에 들어 있는 기보들 저장

profile
끄적끄적 쓰는곳

0개의 댓글