파이썬/파이썬(python) 중급 115

iterator, enumerate, iterrows, zip, iter, next

목차 파이썬에서 iterator이터레이터는 반복가능한 iterable한 객체에 순차적으로 요소를 반환하는 객체이다. iterator은 반복 가능한 객체의 요소를 차례대로 가져오면서 메모리를 절약할 수 있다. 대규모 데이터, 무한 데이터 스트림과 같이 모든 요소를 한 번에 로딩하지 않고, 필요할 때마다 요소를 생성하고 반환하기 때문이다. 파이썬에서 자주 쓰이는 함수 3개를 이야기해보자. iterrows 판다스 데이터프레임에서 각 행을 반복문을 사용하여 출력하는 방법이다. import pandas as pd # 예시 DataFrame 생성 data = {'Name': ['John', 'Emily', 'Ryan'], 'Age': [25, 30, 35], 'City': ['New York', 'Paris', '..

map, pandas, pyplot, matplotlib, code

map(적용할 함수, iterable한 대상) 위의 맵함수를 통해 반환되는 iterator 객체는 list나 tuple과 같은 sequence 자료형으로 변환 가능하다. def square(x): return x ** 2 lst = [1, 2, 3, 4, 5] squared_lst = list(map(square, lst)) print(squared_lst) # 출력: [1, 4, 9, 16, 25] 판다스 unique() df['칼럼명'] df.loc[label] df.iloc[index] -preprocess df_can.drop(['AREA','REG','DEV','Type','Coverage'], axis=1, inplace=True) df_can.rename(columns={"OdName":"C..

pycharm에 tensorflow, keras 설치하기, msvcp140.dll or msvcp140_1.dll 에러, 시스템 운영체제 확인

1.tensorflow 설치하기 하단 터미널을 누른다. pip3 install tensorflow 를 입력한다. 시간이 2~3분 정도 걸린다. 1-1 msvcp140.dll or msvcp140_1.dll 에러 에러가 뜨는 경우가 있다. Could not find the DLL(s) 'msvcp140.dll or msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environment variable. 이 같은 경우 아래의 instruction을 따라가면 된다. You may install these DLLs by downloading "Microsoft C++..

*args 인자가 있는 데코레이터 함수 실습하기

함수 파라미터 앞에 아스테리스크(*)를 붙이면, 함수를 호출할 때 몇 개의 인수라도 허용한다는 뜻이다. 아래의 코드에서 사용자 정의 함수를 만들었는데, 파라미터로 *number를 적어서 여러 개의 파라미터를 가지도록 했다. def calculate(*number): print(f"결과값은 {number[0] + number[1] + number[2]}") calculate를 호출해보자. calculate(1,2,3) >>결과값은 6 이번에는 calculate 함수에 새로운 기능을 더해주는 decorator 함수를 만들 것이다. 아래의 조건을 충족시켜서 데코레이터 함수를 만들어보자. 1. 이름은 decorated라고 한다. 2. 함수의 이름을 호출한다. 3. 첫 번째, 두 번째, 세 번째 인수를 호출한다...

클래스, 데코레이터 함수, *args, **kwargs

User 클래스가 있다. class User: def __init__(self, name): self.name= name self.is_logged_in= False User 클래스에서 new_user 객체를 생성한다. new_user = User("Olivia") user라는 파라미터를 하나 가지는 create_my_post 함수를 만든다. def create_my_post(user): print(f"이것은 {user.name}의 게시글입니다.") 실제로 create_my_post 함수를 호출할 때는, 파라미터로 new_user 객체를 넣는다. create_my_post(new_user) 데코레이터 함수를 만들어보자. is_authenticated_decorator 함수이며, 함수 func을 파라미터로..