[파이썬] pandas DataFrame loc(null값 조회)
새로운 DataFrame을 만듭니다. import pandas as pd raw_data = {'col0': ['a1', 'a2', 'a3', None], 'col1': ['a10', 'a20', 'a30', 'a40'], 'col2': ['a100', None, 'a300', None]} df = pd.DataFrame(raw_data) isna로 null 값을 조회할 수 있습니다. df.loc[df['col0'].isna()] 2개의 column에서 조회는 다음과 같이 합니다. '&' (and)를 사용합니다. 같은 Row에서의 조건이 됩니다. df.loc[df['col0'].isna() & df['col2'].isna()] '|' (or) 사용해서 각각의 다른 Row의 조건을 검색할 수 있습니다. d..
2022. 8. 31.
[파이썬] map 함수 (함수의 반복 실행)
다음과 같이 일반적인 함수를 만들어 보겠습니다. def func1(x): return x*10 func1(10) 결과 100 a_lst = [1,2,3,4,5] result = [] for i in range(len(a_lst)): result.append(func1(a_lst[i])) result 결과 [10,20,30,40,50] map을 사용해 보겠습니다. map(함수, 리스트) 즉, 함수에 인수로 리스트를 넘겨주어 리스트 원소수만큼 반복 실행합니다. a_lst = [1,2,3,4,5] list(map(func1, a_lst)) 결과 [10, 20, 30, 40, 50]
2022. 8. 27.
[파이썬] pandas 행열(Column, Row) 전환
예제로 사용할 데이터를 만듭니다. from pandas import Series, DataFrame raw_data = {'col0': [1, 2, 3, 4], 'col1': [10, 20, 30, 40], 'col2': [100, 200, 300, 400]} data = DataFrame(raw_data) transpose()를 사용하면 Column, Row가 전환됩니다. data2 = data.transpose() Column명을 변경합니다. data2.columns = ['col1','col2','col3','col4'] index를 조회해보면 아래와 같이 나옵니다. 원데이터의 Column 명으로 되어 있습니다. data2.index index 즉 원데이터의 Column명을 다시 'name'이라는 ..
2022. 8. 25.