3
6

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.

Python>pandas>DataFrame:指定行の抽出

Last updated at Posted at 2021-07-31

1.概要

初心者の自分はDataFrameでの行の抽出を練習するときにちょくちょく混乱。。。
今後の混乱予防対策のために少し操作方法を整理。

2.DataFrameの準備

以下のDataFramewoを操作して行の抽出操作を確認する。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(4,3),
                  index=['R1','R2', 'R3', 'R4'],
                  columns=['C1','C2', 'C3'])
C1 C2 C3
R1 0 1 2
R2 3 4 5
R3 6 7 8
R4 9 10 11

3.ひとつの行を抽出

「R2」行のみの以下を抽出する操作を確認。

C1 C2 C3
R2 3 4 5
  • [ ]で囲う操作
df['R2':'R2']
  • .loc.ilocを使う操作
df.loc[['R2']]
df.iloc[[1]]
pd.DataFrame(df.loc['R2']).T

< memo >

df.loc['R2']だけだと出力はSeries型。

C1     3
C2     4
C3     5
Name: R2, dtype: int32

pd.DataFrame()で変換すると縦横逆。

R2
C1 3
C2 4
C3 5

.Tで転置すれば同じ形になった。

  • .dropを使う操作
df.drop(labels =['R1','R3','R4'],axis = 0)
df.drop(labels =[R for R in df.index if R!='R2'],axis = 0)
  • True/Falseを使う操作
df[[False, True, False, False]]
  1. 例えば以下で[False,True,False,False]をつくることが可能。

[input]list(df['C1']==3)

[output][False,True,False,False]
なので以下でも「R2」行のみのDataFrameを抽出できる

df[list(df['C1']==3)]
  1. list()でくくらない場合は出力はSeries型。これを使っても「R2」行のみのDataFrameを抽出できる

[input]list(df['C1']==3)

[output]
R1 False R2 True R3 False R4 False Name: C1, dtype: bool

df[list(df['C1']==3)]
  • df (再掲)
C1 C2 C3
R1 0 1 2
R2 3 4 5
R3 6 7 8
R4 9 10 11

4.ふたつの行を抽出

「R2」「R3」行を抽出して4以下を得る操作を確認。

C1 C2 C3
R2 3 4 5
R3 6 7 8
  • [ ]で囲う操作
df['R2':'R3']
  • .loc.ilocを使う操作
df.loc[['R2','R3']]
df.loc[1:2]
df.iloc[[1,2]]
df.iloc['R2':'R3']
  • .dropを使う操作
df.drop(labels =['R1','R4'],axis = 0)
df.drop(labels =[R for R in df.index 
                 if  R!='R2' and R!='R3'],axis = 0)
  • True/Falseを使う操作
df[[False, True, True, False]]

[ ]の中が以下になるSeriesをつくればよい。

R1 False R2 True R3 True R4 False Name: C1, dtype: bool

例えば以下で上記Seriesが作れるので

  • (df['C1']==3) | (df['C1']==6)
  • (df['C1']<9) & (df['C3']!=2)
df[(df['C1']==3) | (df['C1']==6)]
df[(df['C1']<9) & (df['C3']!=2)]
  • df (再掲)
C1 C2 C3
R1 0 1 2
R2 3 4 5
R3 6 7 8
R4 9 10 11

以上

3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?