LoginSignup
0
1

More than 1 year has passed since last update.

DateFrameのフィルタリング

Posted at

概要

bool型のシーケンスを指定する事でTrueのものだけを取り出す。
変数に対して.loc[df["カラム"] 条件式]とする事で、条件式に当てはまる要素を含む行のDataFrameが作成される。

import numpy as np
import pandas as pd
np.random.seed(0)
colums = ["apple", "orange", "banana", "strawberry", "kiwifruit"]

df = pd.DataFrame()
for colum in colums:
    df[colum] = np.random.choice(range(1, 11), 10)
df.index = range(1, 11)

display(df)
print()

print(df.index % 2 == 0)

display(df[df.index % 2 == 0])
print()

df = df.loc[df["apple"]  >= 5]
df = df.loc[df["kiwifruit"] >= 5]
# df = df.loc[df["apple"]  >= 5][df["kiwifruit"] >= 5]でも可

display(df)

出力結果

image.png

0
1
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
0
1