본문 바로가기

파이썬156

[파이썬] UI (Tkinter) Button, Label, MessageBox Tkinter package를 사용해서 UI를 만드는 간단한 코드 입니다. from tkinter import * import tkinter.messagebox win = Tk() win.geometry("750x270") def view(): tkinter.messagebox.showinfo('타이틀','MessageBox') win.title("Child Window") Label(win, text="Popup MessgeBox", font=('Helvetica 14 bold')).pack(pady=20) Button(win, text= "Show", command=view).pack() win.mainloop() 2022. 9. 22.
[파이썬] pandas 컬럼이 null 이면 다른 컬럼값 변경 Column의 값이 null 일 때, 다른 Column의 값을 변경하고자 합니다. import pandas as pd import numpy as np raw_data = {'age': ['101', '105', '103', '107','108', np.nan,'110','111','112','113'], 'check': ['', '', '', '', '', '', '', '', '', '']} df = pd.DataFrame(raw_data) df 중간에 Null 값이 있는 것을 확인할 수 있습니다. age값이 null 일 때, check Column의 값을 변경하고자 한다면 df.loc[df['age'].isna(), 'check'] = 0 2022. 9. 22.
[파이썬] pygame 으로 원 그리기 import pygame pygame.init() BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) screensize = [400, 300] screen = pygame.display.set_mode(screensize) pygame.display.set_caption("Drawing Rectangle") done = False clock = pygame.time.Clock() while not done: clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: done=True screen.fi.. 2022. 9. 20.
[파이썬] plot으로 원 그리기 import numpy as np import matplotlib.pyplot as plt theta = np.linspace(0, 2*np.pi, 100) radius = 0.3 a = radius*np.cos(theta) b = radius*np.sin(theta) figure, axes = plt.subplots(1) axes.plot(a, b) axes.set_aspect(1) plt.title('Circle using Parametric Equation') plt.show() 2022. 9. 20.
[파이썬] 터틀로 원 그리기 import turtle as t import math radius = 120 circum = 2 * math.pi * radius # math package에 있는 pi 사용 area = math.pi * radius * radius t.setup(width=600,height=600) t.begin_fill() t.color('red', 'yellow') t.shape('turtle') t.circle(radius) t.end_fill() t.done() 2022. 9. 20.
[파이썬] 외부 프로그램 실행 외부의 다른 프로그램을 실행하는 코드입니다. import subprocess run_str = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" subprocess.run(run_str) 2022. 9. 18.
[파이썬] 배열에서 항목(중복제거) 찾고 Count 하기 np.unique를 사용해서 배열 중에 항목(중복제거)을 찾고, 이 항목들의 수를 세는 방법입니다. import numpy as np lst = [1 , 2 , 3 , 9 , 10 , 2 , 2 , 3 , 3 , 1 , 1 ] n_lst = np.array(lst) # 배열을 numpy 배열로 변환 u, c = np.unique(n_lst, return_counts=True) # np.unique 함수 사용 print(u, c) # 참조용 : 유니크한 값, 유니크한 값의 수 print(len(u)) # 몇개의 클래스인지 출력 2022. 9. 18.
[파이썬] 폴더내의 파일명 변경 폴더의 파일 리스트입니다. png 파일명만 수정하고자 합니다. 폴더의 파일 리스트를 보는 코드 입니다. import os import sys for f_name in os.listdir('e:/RnD/tmp/'): print(f_name) 파일 리스트에서 확장자를 제거한 파일명, 확장자를 볼 수 있는 코드입니다. import os import sys from pathlib import Path path = 'e:/RnD/tmp/' for f_name in os.listdir(path): no_extension = Path(f_name).stem ext = f_name[len(no_extension):] print(f_name, ' / ', no_extension, ' / ', ext) 특정 확장자의 파일.. 2022. 9. 18.
[파이썬] pandas 와 csv 파일 연계 csv 파일과 pandas로 연계해서 작업할 수 있습니다. pandas로 데이터를 만듭니다. import pandas as pd import numpy as np # df = pd.DataFrame() df = pd.DataFrame(columns=range(3)) # 3개 column DataFrame 지정 df.columns = ['model','product_type','cnt'] # column명 지정 # df.index.name = 'idx' df.set_index(keys=['model']) # df.loc[len(df)] = ['model001','TV',3] df.loc[len(df)] = ['model002','TV',1] df.loc[len(df)] = ['model003','냉장고',.. 2022. 9. 17.
[파이썬] 가변인자 *args와 **kwargs *가 붙어있어서 C언어를 하신 분들은 포인터와 연관시킬 수 있는데, 파이썬에서는 포인터가 아닙니다. *args : arguments = 함수 인자(arguments)의 값을 받아올 수 있음(튜플) **kwargs : keyword arguments = 함수 인자의 keyword를 받을 수 있음. 이 keyword로 값을 받을 수 있음(튜플) def func(*args, **kwargs): rtn = [] for i in args: # 순자적으로 args의 값들을 가져옴 rtn.append(i) print('*args=',rtn) rtn = [] rtn2 = [] for i in kwargs: # 순차적으로 kwargs의 kwyword들을 가져옴 v = kwargs[i] # keyword를 가지고 값을 찾.. 2022. 9. 16.
728x90
반응형