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 5 years have passed since last update.

indexによるDataFrame/Seriesのgrep

Posted at

仕事中に、DataFrameのindexでgrepしたいことがありました。
列のgrepならすぐわかるのですが、indexの場合少し調べる必要があったので、忘備録として以下に記載しておきます。

DataFrameのgrep

import numpy as np
import pandas as pd

data = np.arange(30).reshape(6,5)
data = pd.DataFrame(data, index=[0,2,4,6,8,12], columns=["A", "B", "C", "D", "E"])
# 個人的にqueryが好きなのでqueryで..
data.query('index in [2,6,12]')

Seriesのgrep

あまり頻度は高くないですが、Seriesでも同様に使いたいことがありました。
ただ、Seriesはそのままではqueryを呼べないので、あまり好きではないですが、以下の方法で呼び出します。

import numpy as np
import pandas as pd

data = np.arange(30).reshape(6,5)
data = pd.DataFrame(data, index=[0,2,4,6,8,12], columns=["A", "B", "C", "D", "E"])
data_se = data["A"]
# queryのように直感でわかる方法を知りたいが..
data_se[data_se.index.isin([0, 6, 12])]

以上です。

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?