[ Python ] How to fast String to pandas data frame 연습

2019. 9. 4. 19:31분석 Python/구현 및 자료

728x90

Original code

 

기존에는 1줄 처리하는데 0.1초 

그리고 중간에 np.place 에서 문제가 있었음

그리고 타입변환은 현재 숫자로 되어있는것이 object로 되어있기때문에 그 부분만 바꿔주면 되는 문제였음

x = np.array(x.split(","))
np.place(x , x == '' , np.nan) ## warning
raw= pd.DataFrane(x.reshpae(1,-1) , columns = col ).astype(type_change)

 

Develop code

0.02초 !!! 5배 향상!!

def type_change(x) :
	try :
    	x = float(x)
    except :
    	x = x
    return x 
    
x2 = 
[
 ( np.nan if re.match(r"\s" , i) is not None else np.nan if len(i) == 0 else type_change(x) for i in x.split(",")
   )
]
raw = pd.DataFrame(x , columns = col)

## list comprehension 사용 안에는 if else 구조와 try except 사용 

 

 

 

728x90