2016-10-23 1 views
2

にDATAFRAME:分割パンダさんは、私は次のような構造を持つデータフレームを持っているとしましょう多くのチャンク

D1
observation 
d1 1 
d2 1 
d3 -1 
d4 -1 
d5 -1 
d6 -1 
d7 1 
d8 1 
d9 1 
d10 1 
d11 -1 
d12 -1 
d13 -1 
d14 -1 
d15 -1 
d16 1 
d17 1 
d18 1 
d19 1 
d20 1 

:D20(ここでは一般化)いくつかの日時インデックスです。

d1:d2、d3:d6、d7:d10などをそれぞれの「チャンク」に分割したい場合は、どうすればいいのですか?

注:

df1 = df[(df.observation==1)] 
df2 = df[(df.observation==-1)] 

は、私が欲しいものではありません。

私はうまくいく方法を考えることができますが、それはうまくいくでしょうが、それほどエレガントではありません。

+0

はどのようにこのデータを得るのですか? ['pandas.read_csv()'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html)は独自の 'chunksize'引数を持っています –

+0

@StevenGこれらは分析値です。これは簡単な例です。基本的には、異なる値を持つシーケンシャルデータがあります。私は、これらの異なる価値グループを新しいデータフレームに分割したいと考えています。またchunksizeは、私が持っていないデータの構造の先験的な知識を必要とするようです。 –

+0

どうやって*チャンクしたいですか?基準は何ですか? ''観察 ''の変化に基づいてあなたはチャンクをしていますか? – wwii

答えて

6

あなたが真の値、新しい値が表示されますので、毎回、新しいグループIDを割り当て、差分IF()がゼロでないobservation列のdiff()cumsum()に基づいてグループ変数を作成することができますcumsum()で作成されます、そして、あなたはdf.groupby((df.observation.diff() != 0).cumsum())...(other chained analysis here)groupby()後に標準分析を適用するか、list-comprehensionで小さなデータフレームにそれらを分割することができ、次のいずれかここ

lst = [g for _, g in df.groupby((df.observation.diff() != 0).cumsum())] 

lst[0] 
# observation 
#d1   1 
#d2   1 

lst[1] 
# observation 
#d3  -1 
#d4  -1 
#d5  -1 
#d6  -1 
... 

インデックスチャンク:

[i.index for i in lst] 

#[Index(['d1', 'd2'], dtype='object'), 
# Index(['d3', 'd4', 'd5', 'd6'], dtype='object'), 
# Index(['d7', 'd8', 'd9', 'd10'], dtype='object'), 
# Index(['d11', 'd12', 'd13', 'd14', 'd15'], dtype='object'), 
# Index(['d16', 'd17', 'd18', 'd19', 'd20'], dtype='object')] 
+0

申し訳ありませんが、これは受け入れるのにとても時間がかかりました。信じられないほど役に立つ。 –

0

実際のdate.datetimeオブジェクトをインデックスとして使用した例です。

import pandas as pd 
import numpy as np 
import datetime 
import random 

df = pd.DataFrame({'x': np.random.randn(40)}, index = [date.fromordinal(random.randint(start_date, end_date)) for i in range(40)]) 

def filter_on_datetime(df, year = None, month = None, day = None): 
    if all(d is not None for d in {year, month, day}): 
     idxs = [idx for idx in df.index if idx.year == year and idx.month == month and idx.day == day] 
    elif year is not None and month is not None and day is None: 
     idxs = [idx for idx in df.index if idx.year == year and idx.month == month] 
    elif year is not None and month is None and day is None: 
     idxs = [idx for idx in df.index if idx.year == year] 
    elif year is None and month is not None and day is not None: 
     idxs = [idx for idx in df.index if idx.month == month and idx.day == day] 
    elif year is None and month is None and day is not None: 
     idxs = [idx for idx in df.index if idx.day == day] 
    elif year is None and month is not None and day is None: 
     idxs = [idx for idx in df.index if idx.month == month] 
    elif year is not None and month is None and day is not None: 
     idxs = [idx for idx in df.index if idx.year == year and idx.day == day] 
    else: 
     idxs = df.index 
    return df.ix[idxs] 

この実行:

>>> print(filter_on_datetime(df = df, year = 2016, month = 2)) 
        x 
2016-02-01 -0.141557 
2016-02-03 0.162429 
2016-02-05 0.703794 
2016-02-07 -0.184492 
2016-02-09 -0.921793 
2016-02-12 1.593838 
2016-02-17 2.784899 
2016-02-19 0.034721 
2016-02-26 -0.142299