Bootcamp/Pandas & python

[Pandas] numpy & pandas 기초 Command 정리

K_Hyul 2023. 10. 10. 09:13
728x90

numpy command

ndarray.ndim # 어레이의 차원
ndarray.shape # 어레이 크기를 나타내는 정수 튜플 행수, 열수
ndarray.size # 요소의 총 개수 shape의 곱과 같음
ndarray.dtype # 데이터 타입 확인


예제 1

lst = [1,4,5,6]
vector = np.array(lst)
vector.shape
vector.dtype




예제 2

arr = np.arange(10)
np.arange(5,10)
np.full((2,3),5) # 모든 원소가 5인 2x3행렬
np.eye(2) # 단위 행렬





예제 3 차원 추가

arr = np.arange(20)
arr.base
arr.shape
arr[:, np.newaxis].shape
arr1 = np.expand_dims(arr, axis=1)





arr.T # transposed


예제 4 슬라이싱/인덱싱

lst = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
arr = np.array(lst)
arr.shape

arr[0,:] = [11, 12, 13]
arr[:,0]
arr[:,::-1]
arr[::2.:]




예제 5 배열 조건 연산

arr = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
arr[arr < 5]

cond = (arr > 2) & (arr < 11)
arr[cond]

 

Pandas command

df = sns.load_dataset('titanic')
df.head()
df.tail()
df.isnull() 널값인지 확인 맞으면 True
df.isnull().sum() # 컬럼별 널 수 확인
df.isnull().sum().sum() # 전체 널 확인
df.info() 전체 수, null 있는 컬럼, 데이터 타입 알 수 있음
df_number = df.slelct_dtypes(include=np.number)
df_object = df.select_dtypes(exclude=np.number)
df.shape
df_number.colums
df_number['age'].median()
df_nmuber.mean()
df_number.mean(1)
df_number.max()
df_number.var()
df_object.columns
df_object['embarked']
df_object['class'].unique()
df.describe(include=np.number)
df.describe(exclude=np.number)
df['adult_male'].describe()

 

 

 

728x90