Python/Pandas&Numpy21

pandas Tricks_04 👉🏻 'Build a DataFrame from multiple files (row-wise & column-wise) ' (Kevin by DataSchool) Q. 여러 csv파일을 가져와 한 개의 dataframe에 나타내고 싶으면? A) 👇 glob package의 glob module 사용! from glob import glob ♣ glob docu 👇👇 ♣ https://docs.python.org/3/library/glob.html ▧ glob module ▧ "The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order(그래서 glob쓰면 sorted() 추천!). No tilde expansion is done, b.. Python/Pandas&Numpy 2022. 3. 25.
map & applymap & apply(on dataframe & Series) 「pandas dataframe에서 유용하게 쓰이는 두 method 'apply' & 'applymap' & 'map' 에 대해서 알아보자」 (+추가로 Series에서의 apply method도 알아보자) ▩ pandas.DataFrame.apply ▩ docu 👉 https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs) "Apply a function along an axis of the DataFrame. Objects passed to the function are Series object.. Python/Pandas&Numpy 2022. 3. 25.
pandas Tricks_03 👉🏻 'Convert Strings→numbers ' (Kevin by DataSchool) Q. 숱하게 많이 보이는 string형 data type...! 하지만 우리는 계산을 위해서 무조건 수치형으로 바꿔야 한다.. 하지만 너무 많은 방법들이 있다. 어떤게 제일 효과적이고 좋은 방법일까? 이 포스팅으로 한 방에 해결하자..! A. 👉🏻 to_numeric 먼저 string type의 수를 모두 입력 (한 개는 -) df = pd.DataFrame({'col_one':['1.1','2.2','3.3'], 'col_two':['4.4','5.5','6.6'], 'col_three':['7,7','8.8','-']}) 1. astype() ♠astype() docu - https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.astype.htm.. Python/Pandas&Numpy 2022. 3. 25.
pandas Tricks_02 👉🏻 'Select columns by Data Type' (Kevin by DataSchool) Q. 오늘도 데이터프레임이 주어졌다. 근데 특정 데이터 타입을 갖는 칼럼만 뽑아내고 싶다.. 어떻게 하면 될까...? A) 👉🏻 'select_dtypes' method 사용! 1> seaborn 'titanic' dataset 불러오면 (seaborn.load_dataset docu 👉🏻 https://seaborn.pydata.org/generated/seaborn.load_dataset.html) import pandas as pd import seaborn as sns titanic = sns.load_dataset('titanic') titanic.head() 2> 먼저 column dtypes 확인! #2) select columns by data type titanic.dtypes 3> se.. Python/Pandas&Numpy 2022. 3. 25.
list comprehension Q. list comprehension? A. list 안에 for문(+if문)을 포함시켜 편리 & 직관적인 프로그래밍 짜기 [f(x) for x in nums] → filter condition 추가도 가능! (if문) [f(x) for x in nums if g(x)] → ex) list안에 있는 element들을 한 줄만으로(👍) element 제곱형태로 표현 가능! nums = [1,2,3,4] result = [x*x for x in nums] #result == [1,4,9,16] nums = [1,2,3,4] result = [x*x for x in nums if x%2 == 0] #result == [4,16] (+) Map + filter → ex) nums = [1,2,3,4] list.. Python/Pandas&Numpy 2022. 3. 23.
pandas functions - cut, qcut ♠'cut' documentation https://pandas.pydata.org/docs/reference/api/pandas.cut.html ♠'qcut' documentation https://pandas.pydata.org/docs/reference/api/pandas.qcut.html 1. cut ✂️ → bin values into discrete intervals. Use cut when you need to segment and sort data values into bins. This function is also useful for going from a continuous variable to a categorical variable. For example, cut could con.. Python/Pandas&Numpy 2022. 3. 23.
pandas Tricks_01 👉🏻 'Reverse (row/column) Order' (Kevin by DataSchool) Q) 데이터프레임이 주어졌다. 근데 내가 원하는 데이터는 행 기준 아래 부분. 데이터 용량이 워낙 커서 데이터 훑기도 힘들다면 A) '행과 열 순서 바꾸기' 👉🏻 .loc[::-1] & .loc[:,::-1] 사용하기 (tail()도 있지만, 데이터프레임 자체의 행/열 구성을 바꿔주지는 않는다!) 1> seaborn 내장 iris dataset 가져오자. import seaborn as sns df_iris = sns.load_dataset('iris') df_iris.head() 2> loc[::-1] 사용. df_iris.loc[::-1].head() 3> 마음에 드는데 index가 걸린다. 다시 index를 reset 해보자(즉, index를 0부터 맞추잔 소리) (reset_index) → 원래 .. Python/Pandas&Numpy 2022. 3. 23.
Pandas 1. 개념 * python library로 구조화된 데이터를 효과적으로 처리하고 저장한다 - array 계산에 특화된 NumPy를 기반으로 설계한다 - 2차원 데이터 외 대용량 데이터 처리에 효과적임 - 데이터 가공, 변환, 정제, 수집, 전처리, 통계, 시각화 작업 모두 가능 -1- 데이터 수집) 텍스트, CSV, Excel, HTML(BeautifulSoup), XML, Hdfs, db, JSON 여러 형태를 pandas는 읽을 수 있다 -2- 데이터 전처리) 전처리 과정을 반드시 거쳐야 함 - 분석가능한 형태로 변형 (null 처리, 이상치, 정규화 등등) -3- 데이터 분석) 통계 & 시각화(matplotlib, seaborn) -4- 예측) pandas에서는 지원하지 않음 - sklearn, T.. Python/Pandas&Numpy 2022. 3. 21.
NumPy intro. + fundamentals 1/2 1. 개념 * NumPy = Numerical Python - Python에서 대규모 다차원 배열을 다룰 수 있게 도와주는 library * 데이터의 대부분은 숫자 배열로 볼 수 있기에 NumPy는 꼭 필요 - 이미지나 소리 등 실생활 대부분이 숫자로 표현됨 * NumPy는 반복문 없이 배열 처리가 가능하며, python list에 비해 빠른 연산을 지원하고 메모리를 효율적으로 사용할 수 있다 * list) list_arr = list(range(5)) print(list_arr) #[0,1,2,3,4] - comma로 구분 print(type(list_arr)) # * numpy) - import 사용 - numpy 배열 생성 및 출력 형태 확인해 보면 ndarray가 생성됨을 확인할 수 있다. - n.. Python/Pandas&Numpy 2022. 3. 20.