2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DaraFrame 行・列の削除

Posted at

概要

変数に対して**.drop( )**にインデックスまたはカラムを指定する事で削除できる。

データフレームを作成

import pandas as pd

data = {"furuits": ["apple", "orange", "banana", "strawberry", "kiwifruit"],
              "time": [1, 4, 5, 6, 3],   "year": [2001, 2002, 2001, 2008, 2006]}

df = pd.DataFrame(data)

image.png

0行目を削除

df1 = df.drop(0)
display(df1)

image.png

列の削除(第2引数にaxis=1を指定する)

df2 = df.drop("year", axis = 1)
display(df2)

image.png

まとめて削除(リストで渡す)

df3 = df.drop([1, 2, 3, 4])
display(df3)

image.png

奇数のインデックスの行を削除

import numpy as np

df4 = df.drop(np.arange(1, 5, 2))
display(df4)

image.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?