pd.drop() 메서드
Pandas.DataFrame.drop
프로젝트를 진행중에 아래와 같은 코드를 만났다.
train.drop([“PassengerId”], axis = 1, inplace = True)자꾸 에러가 나길래, 공부를 좀 해봤다.
출처 : https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html
DataFrame.drop(labels=None, axis=0, index=None, columns=Nome, level=None, inplace=False, errors=’raise’)
Drop은 행과 열로부터 레이블들을 지정한다.
레이블 이름과 대응되는 축을 지정해줌으로써 행과 열을 지운다.
또는 직접적인 인덱스나 열 이름을들 지정해줌으로써 행과 열을 지운다.
다중 인덱스(multi-index)를 사용할 경우 등급을 지정해줌으로써 다른 등급의 레이블도 지울 수 있다.
Parameters
labels
- single label or list-like
- 인덱스 또는 열 레이블을 지운다.
- 튜플은 single label 취급한다. list로 취급하지 않는다.
axis
- 0 or ‘index : 인덱스 자체를 지운다.
- 1 or ‘columns’ : 열 자체를 지운다.
- default는 0
index
- single label or list-like
- axis를 지정해주는 것 대신에
columns
- single label or list-like
- axis를 지정해주는 것 대신에
- 아래 예제에서 확인하자.
level
- int or level name, optional
- 다중인덱스(multiindex)의 경우, 레이블들이 있는 단계가 지워진다
inplace
- bool
- default는 False
- False라면, 복사를 반환하고
- True라면, 덮어씌우고 아무것도 반환하지 않는다.
errors
- ‘ignore’, ‘raise’
- default는 ‘raise’
- ‘ignore’를 넣어주면 에러를 막아주고, 존재하는 레이블만 지워준다.
예제
- 아래 데이터프레임이 있다.
- 아래 데이터프레임에는 name, price, rating, category 칼럼이 있다.
1 | import pandas as pd |
| name | price | rating | category | |
|---|---|---|---|---|
| 0 | coffee | 3000 | 4.0 | 1 |
| 1 | tea | 4000 | 3.5 | 2 |
| 2 | juice | 5000 | 3.7 | 3 |
| 3 | milk | 2000 | 3.0 | 4 |
| 4 | ade | 5000 | 2.0 | 4 |
<script>
const buttonEl =
document.querySelector('#df-03f7980f-018b-4feb-b381-386855cfd372 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-03f7980f-018b-4feb-b381-386855cfd372');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- 여기서 category 칼럼을 지워보자
1 | df.drop(columns = ['category']) |
| name | price | rating | |
|---|---|---|---|
| 0 | coffee | 3000 | 4.0 |
| 1 | tea | 4000 | 3.5 |
| 2 | juice | 5000 | 3.7 |
| 3 | milk | 2000 | 3.0 |
| 4 | ade | 5000 | 2.0 |
<script>
const buttonEl =
document.querySelector('#df-65c3339a-f6d5-470a-96a2-5b43242329b2 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-65c3339a-f6d5-470a-96a2-5b43242329b2');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- 2개 이상의 칼럼을 지울 때는?
1 | df.drop(columns=['category', 'rating']) |
| name | price | |
|---|---|---|
| 0 | coffee | 3000 |
| 1 | tea | 4000 |
| 2 | juice | 5000 |
| 3 | milk | 2000 |
| 4 | ade | 5000 |
<script>
const buttonEl =
document.querySelector('#df-5efa99d7-8bc7-4ac8-a90e-fbd405eaa342 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-5efa99d7-8bc7-4ac8-a90e-fbd405eaa342');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
칼럼을 삭제하는 다른 방법
- 필요한 칼럼만 불러오기
1 | df[['name', 'rating']] |
| name | rating | |
|---|---|---|
| 0 | coffee | 4.0 |
| 1 | tea | 3.5 |
| 2 | juice | 3.7 |
| 3 | milk | 3.0 |
| 4 | ade | 2.0 |
<script>
const buttonEl =
document.querySelector('#df-2a44dfa2-7d6c-4c48-bfa6-7969303817c5 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-2a44dfa2-7d6c-4c48-bfa6-7969303817c5');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
inplace = True
- drop() 메서드에 매개변수를 추가해보자.
- 비교를 위해 먼저, inplace = True를 쓰지 말고 코드를 실행해보자
1 | df.drop(columns=['category']) |
| name | price | rating | |
|---|---|---|---|
| 0 | coffee | 3000 | 4.0 |
| 1 | tea | 4000 | 3.5 |
| 2 | juice | 5000 | 3.7 |
| 3 | milk | 2000 | 3.0 |
| 4 | ade | 5000 | 2.0 |
<script>
const buttonEl =
document.querySelector('#df-5086c77d-5667-4bb0-a196-a74b3665357c button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-5086c77d-5667-4bb0-a196-a74b3665357c');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- category 칼럼이 아주 잘 지워졌다.
- 여기서 다시 원래 데이터프레임을 불러온다면,
1 | display(df) |
| name | price | rating | category | |
|---|---|---|---|---|
| 0 | coffee | 3000 | 4.0 | 1 |
| 1 | tea | 4000 | 3.5 | 2 |
| 2 | juice | 5000 | 3.7 | 3 |
| 3 | milk | 2000 | 3.0 | 4 |
| 4 | ade | 5000 | 2.0 | 4 |
<script>
const buttonEl =
document.querySelector('#df-b24808c0-5065-4c8c-9397-263a4a522c43 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-b24808c0-5065-4c8c-9397-263a4a522c43');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- category 칼럼이 다시 나타나는 것을 볼 수 있다.
- 별도로 df라는 객체에 저장을 안해주었기 때문이다.
inplace 변수를 사용
1 | df.drop(columns=['category'], inplace=True) |
| name | price | rating | |
|---|---|---|---|
| 0 | coffee | 3000 | 4.0 |
| 1 | tea | 4000 | 3.5 |
| 2 | juice | 5000 | 3.7 |
| 3 | milk | 2000 | 3.0 |
| 4 | ade | 5000 | 2.0 |
<script>
const buttonEl =
document.querySelector('#df-6f6d9f85-c19c-465d-ae27-10140142fe5d button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-6f6d9f85-c19c-465d-ae27-10140142fe5d');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- inplace = True는 기존 데이터프레임에 덮어 씌우겠다는 것이다. 따로 저장하는 수고로움을 덜어주는 매개변수인 것이다.
다른 예제
1 | import numpy as np |
| A | B | C | D | |
|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 3 |
| 1 | 4 | 5 | 6 | 7 |
| 2 | 8 | 9 | 10 | 11 |
<script>
const buttonEl =
document.querySelector('#df-8a517d47-71f1-4d40-a5fd-3060a7999df2 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-8a517d47-71f1-4d40-a5fd-3060a7999df2');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
열 지우기
- axis = 1
1 | # axis=1 -> 열 단위로 지워라 |
| A | D | |
|---|---|---|
| 0 | 0 | 3 |
| 1 | 4 | 7 |
| 2 | 8 | 11 |
<script>
const buttonEl =
document.querySelector('#df-8e52a252-c610-46b6-91da-1dd04e7d231a button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-8e52a252-c610-46b6-91da-1dd04e7d231a');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- axis 매개변수 대신에 columns 매개변수를 넣어줘도 동일하다.
1 |
|
| A | D | |
|---|---|---|
| 0 | 0 | 3 |
| 1 | 4 | 7 |
| 2 | 8 | 11 |
<script>
const buttonEl =
document.querySelector('#df-a08dbce1-fcde-4c84-b9e8-c185688bcff1 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-a08dbce1-fcde-4c84-b9e8-c185688bcff1');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
행 지우기
- 인덱스를 통한 방법
1 | df.drop([0, 1]) |
| A | B | C | D | |
|---|---|---|---|---|
| 2 | 8 | 9 | 10 | 11 |
<script>
const buttonEl =
document.querySelector('#df-6b39b6e6-43b6-4a88-b4a0-09c28594ead1 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-6b39b6e6-43b6-4a88-b4a0-09c28594ead1');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
다중 인덱스로 열이나 행 지우기
1 | midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'], |
| big | small | ||
|---|---|---|---|
| lama | speed | 45.0 | 30.0 |
| weight | 200.0 | 100.0 | |
| length | 1.5 | 1.0 | |
| cow | speed | 30.0 | 20.0 |
| weight | 250.0 | 150.0 | |
| length | 1.5 | 0.8 | |
| falcon | speed | 320.0 | 250.0 |
| weight | 1.0 | 0.8 | |
| length | 0.3 | 0.2 |
<script>
const buttonEl =
document.querySelector('#df-9c8e9bb3-86d7-4bfe-8187-b190cbaa4ac9 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-9c8e9bb3-86d7-4bfe-8187-b190cbaa4ac9');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- 위 데이터 프레임에서 ‘falcon’의 ‘weight’를 지우기
1 | df.drop(index=('falcon', 'weight')) |
| big | small | ||
|---|---|---|---|
| lama | speed | 45.0 | 30.0 |
| weight | 200.0 | 100.0 | |
| length | 1.5 | 1.0 | |
| cow | speed | 30.0 | 20.0 |
| weight | 250.0 | 150.0 | |
| length | 1.5 | 0.8 | |
| falcon | speed | 320.0 | 250.0 |
| length | 0.3 | 0.2 |
<script>
const buttonEl =
document.querySelector('#df-fc708903-fd28-49e1-8747-c17b53c42861 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-fc708903-fd28-49e1-8747-c17b53c42861');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- ‘cow’ 인덱스와 ‘small’ 열 지우기
1 | df.drop(index='cow', columns='small') |
| big | ||
|---|---|---|
| lama | speed | 45.0 |
| weight | 200.0 | |
| length | 1.5 | |
| falcon | speed | 320.0 |
| weight | 1.0 | |
| length | 0.3 |
<script>
const buttonEl =
document.querySelector('#df-0268e32a-e810-4e90-9aa7-ee48a261a652 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-0268e32a-e810-4e90-9aa7-ee48a261a652');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
- 모든 인덱스에 ‘length’ 지우기
1 | df.drop(index='length', level=1) |
| big | small | ||
|---|---|---|---|
| lama | speed | 45.0 | 30.0 |
| weight | 200.0 | 100.0 | |
| cow | speed | 30.0 | 20.0 |
| weight | 250.0 | 150.0 | |
| falcon | speed | 320.0 | 250.0 |
| weight | 1.0 | 0.8 |
<script>
const buttonEl =
document.querySelector('#df-66624418-0a6c-42d1-8fb4-aa14716959d2 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-66624418-0a6c-42d1-8fb4-aa14716959d2');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
</script>
</div>
pd.drop() 메서드
You need to set
install_url to use ShareThis. Please set it in _config.yml.Like this article? Support the author with
Comments
You forgot to set the
shortname for Disqus. Please set it in _config.yml.