반응형
import numpy as np
import pandas as pd
data = np.random.rand(10,5)
indices = list(range(101,111))
columns = ['a','b','c','d','e']
df = pd.DataFrame(data=data,columns=columns,index=indices)
print(df)
print(df.drop('a', axis=1)) # default = 행방향 --> axis =1 열방향
print(df) # 그대로
df = df.drop('a', axis=1) # or df.drop('a', axis=1, inplace = True)
print(df.drop(['b','c'],axis=1))
df = df.drop(['b','c'],axis=1)
print(df.drop(104,axis=0)) # 0 : 행, 1 : 열
print(df.drop([101,102],axis=0))
print(df[~(df['e']>0.5)])
print(df.drop((df['e']>0.5).index, axis=0))
#결측치 제거
df = pd.DataFrame(data=data,columns=columns,index=indices)
data[4,2] = np.nan
data[2,4] = np.nan
data[7,3] = np.nan
print(df)
print(df.isna()) # nan 찾아줌
print(df.notna()) # nan 아닌 것 찾아
print(df.fillna(-1)) # nan --> -1
print(df.dropna(axis=0)) # 결측치가 있는 행을 제거
print(df.dropna(axis=1)) # 결측치가 있는 열을 제거
print(df.dropna(how = 'all', axis=0)) # 행이 모두 nan일 때, 그 행을 제거
print(df.dropna(thresh=4,axis=0))
print(df.dropna(thresh=5,axis=0))
print(df.dropna(subset=['a','b'],axis=0)) # a와 b열에 결측치 있으면 제거, 나머지는 상관 x
반응형
'데이터 분석' 카테고리의 다른 글
DataFrame의 연산 - map과 apply (0) | 2022.08.15 |
---|---|
DataFrame의 연산 - 인덱스 설정 (0) | 2022.08.15 |
Pandas dataframe의 연산 - 1 (0) | 2022.08.14 |
Pandas Dataframe 클래스 (0) | 2022.08.13 |
Pandas 패키지 (0) | 2022.08.12 |
댓글