4
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.

pandasで欠損値nanじゃないデータを抽出する方法

Posted at

pandasで欠損値nanじゃないデータを抽出する方法

ちょうど千葉県 Go To EATの加盟店一覧の抽出方法考えてたところだったので記事を参考にまとめました

import pandas as pd
import io

data = """
名前,回数,開始,終了
ぽんすけ,1,9:00,18:00
ぽんすけ,2,18:00,
ぽんすけ,3,9:00,13:00
ぽんすけ,4,,
ぽんすけ,5,9:00,
ぽんすけ,6,18:00,
ぽんすけ,7,12:00,
ぽんすけ,8,12:00,
ぽんすけ,9,,18:00
ぽんすけ,10,,
"""

df = pd.read_csv(io.StringIO(data))

df
名前 回数 開始 終了
0 ぽんすけ 1 9:00 18:00
1 ぽんすけ 2 18:00 nan
2 ぽんすけ 3 9:00 13:00
3 ぽんすけ 4 nan nan
4 ぽんすけ 5 9:00 nan
5 ぽんすけ 6 18:00 nan
6 ぽんすけ 7 12:00 nan
7 ぽんすけ 8 12:00 nan
8 ぽんすけ 9 nan 18:00
9 ぽんすけ 10 nan nan

1つのカラムがNaNじゃないデータを抽出する

df[(df.loc[:, "開始"].notnull() == True)]

2つのカラム両方NaNじゃないデータを抽出する

df[(df.loc[:, ["開始", "終了"]].notnull() == (True, True)).all(axis=1)]

2つのカラムどっちかがNaNじゃないければデータを抽出する

df[(df.loc[:, ["開始", "終了"]].notnull() == (True, True)).any(axis=1)]

「1つのカラムがNaNじゃない」&「1つのカラムがNaN」のデータを抽出する

df[(df.loc[:, ["開始", "終了"]].notnull() == (False, True)).all(axis=1)]
4
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
4
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?