우선 데이터프레임을 생성합니다.
my_dictionary = {"col1":[0,1,2,3], "col2": pd.Series([1,2,3],index=[1,2,3])}
df=pd.DataFrame(data=my_dictionary, index=[0,1,2,3])
df
Drop 명령어를 사용해서 행,열을 제거 가능합니다.
1. 행 제거
df.drop([0])
또는 index를 직접 지정하는것도 가능합니다.
df.drop(index=0)
2. 열제거
두가지 방법이 존재합니다. axis=1을 설정하는 방법과 columns=[] 를 사용하는 방법이 있습니다.
df.drop(['col1'],axis=1)
df.drop(columns = ['col1'])
결과는 동일합니다.
3. Inplace
inplace를 사용하지 않는경우 실제데이터에 drop한 결과가 반영되지 않습니다.
사용하지 않은경우 (inplace = False)
df.drop(['col1'],axis=1)
df
사용한 경우 (inplace =True)
df.drop(['col1'],axis=1,inplace=True)
df
※참고
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop.html
pandas.DataFrame.drop — pandas 1.4.3 documentation
next pandas.DataFrame.drop_duplicates
pandas.pydata.org
'Others > Data Science' 카테고리의 다른 글
Preprocessing (2) 누락된 값의 처리(Null) (0) | 2022.07.15 |
---|---|
Pandas - DataFrame (3) 조회(loc/iloc) (0) | 2022.07.15 |
Preprocessing (1) train_test_split (0) | 2022.07.14 |
Pandas - DataFrame (1) 생성 (0) | 2022.07.14 |
Machine Learning (1) 머신러닝 성능 평가 지표 (회귀) (3) | 2022.07.11 |