0
0

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.

【メモ】Pandas 特定の条件の行を抽出する

Posted at

はじめに

表形式のデータをきれいにする時に特定の条件の行を抽出したくなる時などがあると思います。

今回はそんな時に使えそうなpandasのquery()関数を紹介します。

書式

書式はシンプルでquery()の引数に条件式を書いてあげるだけです。

df.query(条件)

実際のコード

tohoku.py
import pandas as pd
import numpy as np
a_values = [1,2,3,4,5,6]
b_values = ["Aomori","Akita","Iwate","Miyagi","Yamagata","Fukushima"]
c_values = [129,96,122,230,108,184]
my_dict = {"番号":a_values, "県名":b_values, "人口":c_values}

df = pd.DataFrame.from_dict(my_dict)
df
tohoku.py
#人口が100万人より多い県を抽出する
print(df.query('人口 > 100'))
tohoku.py
#人口100万人未満の県を抽出する
print(df.query('人口 > 100'))

Google Colaboratoryはこちらから

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?