2016-10-16 8 views

答えて

3

A列の変更を検出するのにcumsumを使用すると、booleanというようにpythonを集計できます。

# Test data 
df= DataFrame([True, True, False, False, False, False, True, False, False], 
       index=pd.to_datetime(['2015-05-01', '2015-05-02', '2015-05-03', 
            '2015-05-04', '2015-05-05', '2015-05-06', 
            '2015-05-07', '2015-05-08', '2015-05-09']), 
       columns=['A']) 

# We have to ensure that the index is sorted 
df.sort_index(inplace=True) 
# Resetting the index to create a column 
df.reset_index(inplace=True) 

# Grouping by the cumsum and counting the number of dates and getting their min and max 
df = df.groupby(df['A'].cumsum()).agg(
    {'index': ['count', 'min', 'max']}) 

# Removing useless column level 
df.columns = df.columns.droplevel() 

print(df) 
# count  min  max 
# A        
# 1  1 2015-05-01 2015-05-01 
# 2  5 2015-05-02 2015-05-06 
# 3  3 2015-05-07 2015-05-09 

# Getting the max 
df[df['count']==df['count'].max()] 

# count  min  max 
# A        
# 2  5 2015-05-02 2015-05-06 
+0

素晴らしいが、私は、その指標として「日付」を使用しカントと私はdf.indexしようとする代わりに、私はTypeError例外を取得:非ハッシュタイプ:「DatetimeIndex」 –

+0

私のDFは、実際にデータフレームを持っているSDATAと呼ばれるオブジェクトでありますメンバーとして 'df'。だから、もし私が 'df.index'または 'sData.df.index'を試しても、私は 'index'を試してみるとエラーになる。私もエラーが出る。私はどのように書くべきかを知っていませんが、agg関数に 'index'を書きます。 –

+0

@RunnerBean私の例で示すように、インデックスを最初に 'df.reset_index(inplace = True)'にリセットする必要があります。 – Romain

関連する問題