質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
date

dateは、date型や日付に関する関数や処理についてのタグです

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

1回答

1259閲覧

Pyrthon pandas 時間範囲指定 抽出

icemanstanding

総合スコア73

date

dateは、date型や日付に関する関数や処理についてのタグです

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2021/06/11 07:13

添付のようなデータdiffから、時間範囲を指定してデータを抽出したいのですが、
アドバイスをお願いします。

例えば、0 days 00:00:00から0 days 00:03:00以内データを抜きとりたい。

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

まず、DataFrameから条件を満たす行を取り出す方法は以下です。

python

1>>> df_sample = pd.DataFrame({'name':['山田', '谷川', '海野'], 'score1':[80, 80, 60], 'score2':[40, 70, 70], }) 2>>> 3>>> df_TF = (df_sample['score1']==80) & (df_sample['score2']==70) 4>>> print(df_TF) 50 False 61 True 72 False 8dtype: bool 9>>> df_SELECT = df_sample[df_TF] 10>>> print(df_SELECT) 11 name score1 score2 121 谷川 80 70

一行でやると以下になります。

python

1>>> df_SELECT = df_sample[(df_sample['score1']==80) & (df_sample['score2']==70)] 2>>> print(df_SELECT) 3 name score1 score2 41 谷川 80 70

次にDataFrameの要素の型の調べ方は以下です。

python

1>>> print(df) 2 0 30 0 days 03:43:32 41 0 days 00:00:00 52 0 days 00:01:00 63 0 days 00:01:00 74 0 days 00:03:00 8>>> print(type(df.iloc[0,0])) 9<class 'pandas._libs.tslibs.timedeltas.Timedelta'> 10>>> Timedelta = type(df.iloc[0,0]) 11>>> print(Timedelta.__doc__) 12 13 Represents a duration, the difference between two dates or times. 14 15 Timedelta is the pandas equivalent of python's ``datetime.timedelta`` 16 and is interchangeable with it in most cases. 17 18 Parameters 19 ---------- 20 value : Timedelta, timedelta, np.timedelta64, str, or int 21 unit : str, default 'ns' 22 Denote the unit of the input, if input is an integer. 23 24 Possible values: 25 26 * 'W', 'D', 'T', 'S', 'L', 'U', or 'N' 27 * 'days' or 'day' 28 * 'hours', 'hour', 'hr', or 'h' 29 * 'minutes', 'minute', 'min', or 'm' 30 * 'seconds', 'second', or 'sec' 31 * 'milliseconds', 'millisecond', 'millis', or 'milli' 32 * 'microseconds', 'microsecond', 'micros', or 'micro' 33 * 'nanoseconds', 'nanosecond', 'nanos', 'nano', or 'ns'. 34 35 **kwargs 36 Available kwargs: {days, seconds, microseconds, 37 milliseconds, minutes, hours, weeks}. 38 Values for construction in compat with datetime.timedelta. 39 Numpy ints and floats will be coerced to python ints and floats. 40 41 Notes 42 ----- 43 The ``.value`` attribute is always in ns. 44 45 If the precision is higher than nanoseconds, the precision of the duration is 46 truncated to nanoseconds.

datetime.timedeltaとほぼ同じだと書いてあるので、これを使えば0 days 00:03:00を作ることができます。

これらを組み合わせれば抜き出すことができるので、がんばってください。

投稿2021/06/12 10:59

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問