[Python] String 을 List 형식으로 바꾸는 방법, String to List(+ String List to List)
1. str.split('seperator') List 로 변환할 때 구분할 수 있는 Seperator 가 있는 경우 사용 split() 에서 괄호 사이에 '/', ';' 등 구분자가 되는 기호나 문자를 넣으면 그 구분자를 기준으로 List 로 만들어 줌 str = 'apple, orange, banana' pring( str.split(',') ) >> ['apple', 'orange', 'banana'] 2. list(str) String 의 모든 글자를 하나씩 쪼개어 List 를 만들 때 사용 str = 'apple, banana' print( list(str) ) >> ['a', 'p ', 'p', 'l', 'e', ',', ' ', 'b', 'a', 'n', 'a', 'n', 'a'] 2. js..
2022. 12. 16.
[Python] Pandas dataframe 합치기 (concat, merge, join)
#결과 B C D A a1 b1 c1 d1 a2 b2 c2 d2 1. concat import pandas as pd df1 = pd.DataFrame({'A':['a1', 'a2', 'a3'], 'B':['b1', 'b2', 'b3'], 'C':['c1', 'c2', 'c3']}) df2 = pd.DataFrame({'A':['a4', 'a5', 'a6'], 'D':['d1', 'd2', 'd3']}) 위 데이터 프레임을 사용해 연습해보자 result = pd.concat([df1, df2]) print(result) # 결과 A B C D 0 a1 b1 c1 NaN 1 a2 b2 c2 NaN 2 a3 b3 c3 NaN 0 a4 NaN NaN d1 1 a5 NaN NaN d2 2 a6 NaN NaN d..
2021. 11. 29.
[Python] Pandas dataframe 'isin' & 'not in' 사용법
1. IN 데이터 프레임 A와 B에서 공통되는 부분을 찾고 싶은 경우 in 을 사용할 수 있다. 간단한 예제를 통해 in을 사용하는 경우를 살펴보자 A = pd.DataFrame({'fruits': ['strawberry', 'banana', 'cranberry', 'apple', 'orange'] , 'price': ['1000', '2000', '1500', '500', '2500']}) B = ['strawberry', 'cranberry'] A는 fruits, price 두 개의 열로 이루어진 데이터 프레임 이며, B는 리스트이다. A의 fruits에서 B에 있는 과일만 골라서 보고싶은 경우, 아래처럼 코드를 작성할 수 있다. df = A[A.fruits.isin(B)] 위 코드를 작성할 경우 B..
2021. 11. 29.