我試圖將分組數據降採樣到每日平均值,計算每組,並繪製在一個圖中得到的時間序列。 我的出發點是以下pd.DataFrame
:Groupd下采樣和繪圖pd.DataFrame
value time type
0.1234 2013-04-03 A
0.2345 2013-04-05 A
0.34564 2013-04-07 A
... ... ...
0.2345 2013-04-03 B
0.1234 2013-04-05 B
0.2345 2013-04-07 C
0.34564 2013-04-07 C
我想每天計算裝置,每種類型的內容,並繪製時間序列這些日常手段的單一情節。
目前,我有這個...
names = list(test['type'].unique())
types = []
for name in names:
single = df.loc[df.type == name]
single = single.set_index(single.time, drop=False)
single = single.resample("D")
types.append(single)
for single, name in zip(types, names):
single.rename(columns={"value":name}, inplace=True)
combined = pd.concat(types, axis=1)
combined.plot()
...產生含有所需的輸出組合數據幀和下面的情節:
在我看來,這可能是通過在初始數據幀上使用groupby
更容易實現,但到目前爲止,我還無法使用此方法重現所需的繪圖。
什麼是「聰明的方式」來做到這一點?
編輯: 更大的數據樣本(CSV,1000行)在:http://pastebin.com/gi16nZdh
感謝, 馬蒂亞斯
您能否提供更大的示例數據集?在csv格式的pastebin中。 – Ffisegydd 2014-10-01 12:46:23
從數據框中添加1k行csv隨機樣本。 – Matthias 2014-10-01 13:06:44
它當然有幫助,謝謝。我必須將整個數據集的'pivot'轉換爲'pivot_table',但您肯定指出了正確的方向。 – Matthias 2014-10-01 13:38:52