기록중

2021.07.26 오늘의 코드

lian_is_clone 2021. 7. 26. 18:43

허허헛 그래프큐엘 차분히 배우자...

 

오늘은 간단한 pandas 조건부 np.where 를 배웠다.

 

한가지 조건은 고객 리스트에 해당하는 고객이 참여하였는가 여부를 판단하는것이였다.

 

import pandas as pd
import numpy as np

customerFiles = pd.read_excel("file path")

checkList = ["jeck", "mike", "taler", "aily", ... ]

customerFiles["check"] = np.where(customerFiles["customer"].str.contains("|".join(checkList)),"yes","no")

or

customerFiles["check"] = np.where(customerFiles["customer"] == checkList,"yes","no")

이렇게 되면 checkList에 이름이 포함되어있는 사람은 모두 체크가 된다.

 

이제 한개 클리어 

 

두번째는 참여한 사람들이 얼마만큼 구매를 하였는지 확인을 한다.

 

def twoColMultiply(data, col1,col2, list1, list2):
    condlist = [ (data[col1].str.contains(pat=requirement, regex=False)) for requirement in list1]
    choicelist = [ (list2[value] * data[col2]) for value in range(len(list1))]
    _calculator = np.select(condlist, choicelist, default=0)
    return _calculator.sum()

customerFiles["Values"] = [0 for i in range(len(customerFiles))]

for user in checkList:
    userex = user.repalace(user,user+".xlsx")
    datas = pd.read_excel("path/"+userex)
    
    customerFiles["Values"] = np.where(customerFiles["customer"].str.contains(user),twoColMultiply(datas,"col1","col2",["optionList"],["valueList"]),customerFiles["Values"])

총가격계산하는함수를 미리 만들었던적이 있어서 이용하였다.    

 

처음엔 포문 안쓰고 만들려다 실패.... 그래도 데이터가 얼마 없어서 잘돌아갔다...

 

이다음엔 실적별로 등급을 만들려고 하였다. 

 

인터넷에 찾아보니 아주 능력자들은 많이 있었다. 간단하게 만들어야지 ㅎㅎㅎ

 

customerFiles["Tiers"] = pd.cut(customerFiles["Values"], [-1,70,80,90,np.inf],labels=["F","C","B","A"]

print(customerFiles)

처음 -1자리에 0을 입력하였더니 0은 포함을 안한다. 

 

해석하자면 값들을 가져와

0부터 ~ 70까지 "F"

71부터 ~ 80까지 "C"

81부터 ~ 90까지 "B"

91부터 ~ 모든수 까지 "A"다 

 

역시 pd는 짱이야 

 

오전은 보고서 업무하고 오후에 이 코드로 보고용 파일 또 만들고 흠

코드가 조금씩 쌓여가고 있지만 너무 적게 쌓이는거 같은데 흠...

 

 

'기록중' 카테고리의 다른 글

2021.08.05 오늘의 코드  (0) 2021.08.05
2021.08.03 오늘의 코드  (0) 2021.08.03
2021.07.21 오늘의 코드  (0) 2021.07.21
2021.07.16 오늘의 코드(python, pandas, numpy)  (0) 2021.07.16
2021.07.15 오늘의 한일  (0) 2021.07.15