パンダにDateTimeインデックス付きのデータフレームがあります。タイムpd.Grouper(freq='360Min')
グループでグループ化する場合:、この結果を元のタイムスタンプに結合するにはどうすればよいですか?つまり、等結合のタイムスタンプ=バケットは機能しませんか?便利な機能はありますか?asof
結合を使用する必要がありますか?または、手動で時間を抽出して、それを一致させようとする必要がありますか?
のソースのために
import pandas as pd
df = pd.DataFrame(
{
"Publish date": [
pd.Timestamp("2000-01-02"),
pd.Timestamp("2000-01-02"),
pd.Timestamp("2000-01-09"),
pd.Timestamp("2000-01-16")
],
"ID": [0, 1, 2, 3],
"Price": [10, 20, 30, 40]
}
)
それは与える:
Publish date ID Price
0 2000-01-02 0 10
1 2000-01-02 1 20
2 2000-01-09 2 30
3 2000-01-16 3 40
任意の頻度(月、日、時間だけでなく)で集計を実行したい、たとえば1
month.
agg_result = df.groupby(pd.Grouper(key="Publish date", freq="1M")).agg([pd.Series.mean, pd.Series.median]).reset_index()
agg_result.columns = ['_'.join(col).strip() for col in agg_result.columns.values]
agg_result.columns = ['Publish date month', 'ID_mean', 'ID_median', 'Price_mean', 'Price_median']
print(agg_result)
Publish date month ID_mean ID_median Price_mean Price_median
0 2000-01-31 1.5 1.5 25 25
エクイジョインが再び機能することを確認するにはどうすればよいですか?つまり、同じ任意の頻度を使用して、元のタイムスタンプをフィッティングバケットに変換しますか?
つまり、例のコードで説明されているように、どうすれば取得できますか?
agg_result['Publish date month'] = agg_result['Publish date'].apply(magic transform to same frequency bucket)
df.merge(agg_result, on['Publish date month'])
動作するには、つまり、適切なバケットへの変換を定義しますか?
編集:
各グループに対応する元の値を識別する最も簡単な方法は、次のとおりです。
gb = df.groupby(pd.Grouper(key="Publish date", freq="1M"))
dict(list(gb['Publish date']))
次に、これを使用して、任意の情報を元のテーブルに結合し直すことができます。
2つの中間列で結合できますか?
df['Publish date'].dt.month
そして
df.groupby(pd.Grouper(key="Publish date", freq="1M")).agg([pd.Series.mean, pd.Series.median]).index.month
このような
results = df.groupby(pd.Grouper(key="Publish date", freq="1M")).agg([pd.Series.mean, pd.Series.median])
results.columns = ['-'.join(col[::-1]).strip() for col in results.columns]
df['month'] = df['Publish date'].dt.month
results['month'] = results.index.month
results.merge(df)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加