Pandas: データフレームについて--04: 行，列の削除

# 行や列を削除する `.drop()`

```
drop(labels=None, axis: 'Axis' = 0, index=None, columns=None,
    level: 'Level | None' = None, inplace: 'bool' = False,
    errors: 'str' = 'raise')
```

pandas 0.21.1 以降では `labels` と `axis` で指定していたものが `index`, `columns` で指定できるようになった。

新しいバージョンをインストールして，`index`, `columns` を使おう。


```python
import pandas as pd
df = pd.DataFrame({
    'a': [1, 2, 3, 4, 5],
    'b': [1.2, 3.4, 5.6, 7.8, 9.0],
    'c': ['A', 'B', 'C', 'D', 'E']
})
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>1.2</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>5.6</td>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>7.8</td>
      <td>D</td>
    </tr>
    <tr>
      <th>4</th>
      <td>5</td>
      <td>9.0</td>
      <td>E</td>
    </tr>
  </tbody>
</table>
</div>



## 1.　列を削除する

`drop(labels=列名, axis=1)`
`drop(columns=列名)`


```python
df.drop(labels='a', axis=1)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.2</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>5.6</td>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>7.8</td>
      <td>D</td>
    </tr>
    <tr>
      <th>4</th>
      <td>9.0</td>
      <td>E</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.drop(columns='a')
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.2</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>5.6</td>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>7.8</td>
      <td>D</td>
    </tr>
    <tr>
      <th>4</th>
      <td>9.0</td>
      <td>E</td>
    </tr>
  </tbody>
</table>
</div>



複数列を削除する場合には，リストで指定する。


```python
df.drop(labels=['a', 'b'], axis=1)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>D</td>
    </tr>
    <tr>
      <th>4</th>
      <td>E</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.drop(columns=['a', 'b'])
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>D</td>
    </tr>
    <tr>
      <th>4</th>
      <td>E</td>
    </tr>
  </tbody>
</table>
</div>



## 2.　行を削除する

`drop(labels=インデックス, axis=0)`
`drop(index=インデックス,)`


```python
df.drop(labels=4, axis=0)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>1.2</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>5.6</td>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>7.8</td>
      <td>D</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.drop(index=4)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>1.2</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>5.6</td>
      <td>C</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>7.8</td>
      <td>D</td>
    </tr>
  </tbody>
</table>
</div>



複数行を削除する場合には，リストで指定する。


```python
df.drop(labels=[0, 2, 4], axis=0)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>7.8</td>
      <td>D</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.drop(index=[0, 2, 4])
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3.4</td>
      <td>B</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>7.8</td>
      <td>D</td>
    </tr>
  </tbody>
</table>
</div>



## 3.　行と列を同時に削除する

`index` と `columns` を同時に使用することができる。


```python
df.drop(index=[0, 2, 4], columns='c')
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>3.4</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>7.8</td>
    </tr>
  </tbody>
</table>
</div>



## 4.　もっと知りたい人

```
help(df.drop) # Help on method drop in module pandas.core.frame
```