코드 스니펫

개인적인 참고 용도의 코드 모음집

 

Pandas

df['colname'] = df['colname'].astype(int)
apodp["Grouped Article"] = apodp["Grouped Article"].map(int)

df = merged.pivot_table(values=self.month, index=['Sales rep', 'Sold-to', 'Grouped Article'], aggfunc='sum')
df.reset_index(inplace=True)
df["concat"] = df["Sold-to"].apply(str)+df["Grouped Article"]
df = df.reindex(columns=["concat", "Sold-to", "Sales rep", "Grouped Article", self.month])

fc_only_with_pic = self.apo_dp_df[self.apo_dp_df.apply(lambda x: x['Sales rep'] in self.sales_rep_list, axis=1)]
makt_df["MATNR"] = makt_df["MATNR"].apply(lambda x: x.lstrip('0'))

 

 

 

git checkout -b <branch 이름>

 

 

 

random.choice(["A", "B"]))​

 

import time

start_time = time.time()
end_time = time.time()

print(end_time - start_time)

 

 

아나콘다를 이용한 환경 설정 방법.

  • 파이썬 3.6버전 가상환경 만들기

(base) C:\Users\user-name> conda create -n py36 python=3.6

 

  • 패키지 전체 설치하기.

(py36) C:\Users\user-name> pip install -r requirements.txt

 

 

pip show tensorflow

패키지 버전 확인

 

(py36) C:\Users\user-name> pip install pandas

(py36) C:\Users\user-name> conda install pandas

  • 아나콘다 특정 버전 패키지 설치하기.

(py36) C:\Users\user-name> conda install pandas==0.25.2

(py36) C:\Users\user-name> pip freeze > requirements.txt

 

 

zip, zip_longest

  • Takes iterables (lists, dictionaries, tuples, strings, etc.) and "zips" them into tuples.
  • Returns a zip object, which is an iterator of tuples
from itertools import zip_longest

countries = ["Ecuador", "Perú", "Colombia", "USA", "Chile"]
capitals = ["Quito", "Lima", "Bogotá", "Washington DC"]
cities = ["Guayaquil", "Trujillo", "Medellín", "New York"]
countries_and_capitals = zip_longest(countries, capitals, cities, fillvalue="Valparaíso")

list(countries_and_capitals)

 

enumerate

for index, element in enumerate(fruits, start=1):
    filename = f"file{index}.jpg"
    # do something with the file and the element
    print(filename)

 

 

 

Sort vs Sorted

 

The sort method only works for lists. -> inplace
The sorted function works for any iterable. -> do not change argument
You can specify the parameter reverse in both of them.
And you can also specify a key.

 

fruits = {"apple": 40, "watermelon": 50, "pineapple":10, "grapes":15}

sorted(fruits)
# ['apple', 'grapes', 'pineapple', 'watermelon']
sorted(fruits, key=lambda fruit: fruits[fruit])
# ['pineapple', 'grapes', 'apple', 'watermelon']

 

 

List Comprehension

 

 

Designed by JB FACTORY