1
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 1 year has passed since last update.

pandasメモ(DataFrameの操作)

Last updated at Posted at 2021-03-11

サンプルデータ

import pandas as pd
from sklearn.datasets import load_iris

# データ取得(あやめの花のデータセット)
iris = load_iris()
data = iris.data
columns = iris.feature_names

# pd.DataFrame
df = pd.DataFrame(data=data, columns=columns)

データの確認

df.head() # 先頭を5件を表示

df.shape # 行数、列数を表示

カラム名の変更

# カラム名をリストで定義
columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
df.columns = columns

特定の要素を抽出

iloc (要素番号を指定)

# 11行〜20行目までを取得
df.iloc[10: 20] # df.iloc[10: 20, : ]と同じ
# 11行〜20行目、2〜3列目までを取得
df.iloc[10: 20, 1: 3]
# 1〜3列目までを取得
df.iloc[: , : 3]

loc(columns, indexを指定)

# 11行〜20行目までを取得
df.loc[11: 20]
# 11行〜20行目、2〜3列目までを取得
df.loc[11: 20, 'sepal_width': 'petal_length']
# 1〜3列目までを取得
df.loc[: , : 'petal_length']

特定の列を抽出

df['sepal_length'] # 戻り値の型は、pandas.core.series.Series

要素の条件で抽出

sepal_length >= 5.0

df[df['sepal_length'] >= 5.0]

df.query('sepal_length >= 5.0')

sepal_length >= 5.0 かつ sepal_width >= 3.0

df[(df['sepal_length'] >= 5.0) & (df['sepal_width'] >= 3.0)]

df.query('sepal_length >= 5.0 & sepal_width >=3.0')

ソート

データ(要素)でソート

# sepal_lengthでソート(ascending=Trueは昇順)
df.sort_values(by='sepal_length', ascending=True)

インデックスでソート

# ascendingは昇順/降順
# indexでソート
df.sort_index(ascending=False)
# columnsでソート
df.sort_index(axis=1, ascending=True)
1
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
1
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?