2017-07-20 258 views
1

我有以下的數據框(有日期時間指數):在大熊貓列的子集的日平均數據幀

   col_a col_b col_c col_d col_e col_f col_g col_h fid 
7/20/2017 10:00 0  18 45 17 19 2.777778 180 0.92 999000 
7/20/2017 11:00 0.03 18 45 17 19 2.2222224 180 0.93 999000 
7/20/2017 12:00 0.03 18 45 17 19 2.2222224 180 0.95 999000 
7/20/2017 13:00 0.03 17 45 17 19 2.2222224 180 0.95 999000 
7/20/2017 14:00 0.04 17 45 17 19 1.6666668 180 0.97 999000 
7/20/2017 15:00 0.03 17 45 17 19 1.6666668 180 0.97 999000 
7/20/2017 16:00 0.02 17 45 17 19 1.6666668 157.5 0.97 999000 
7/20/2017 17:00 0.01 17 45 17 19 1.6666668 135 0.97 999000 
7/20/2017 18:00 0.01 17 45 17 19 1.6666668 157.5 0.97 999000 
7/20/2017 19:00 0.02 17 45 17 19 1.6666668 157.5 1 999000 
7/20/2017 20:00 0.01 17 45 17 19 2.2222224 135 1 999000 
7/20/2017 21:00 0.01 18 45 17 19 2.2222224 135 1 999000 
7/20/2017 22:00 0.01 18 45 17 19 2.777778 157.5 0.98 999000 
7/20/2017 23:00 0.03 19 45 17 19 2.777778 157.5 0.96 999000 
7/21/2017 0:00 0.04 19 45 16 21 3.0555558 157.5 0.92 999000 
7/21/2017 1:00 0.05 20 45 16 21 3.8888892 157.5 0.88 999000 
7/21/2017 2:00 0.03 21 45 16 21 3.8888892 157.5 0.83 999000 
7/21/2017 3:00 0.02 21 45 16 21 3.8888892 157.5 0.8 999000 
7/21/2017 4:00 0.03 21 45 16 21 4.4444448 157.5 0.78 999000 
7/21/2017 5:00 0.03 21 45 16 21 4.4444448 157.5 0.79 999000 
7/21/2017 6:00 0.02 21 45 16 21 3.8888892 157.5 0.83 999000 
7/21/2017 7:00 0.03 20 45 16 21 3.6111114 135 0.86 999000 
7/21/2017 8:00 0.04 19 45 16 21 3.0555558 157.5 0.91 999000 
7/21/2017 9:00 0.03 18 45 16 21 2.777778 157.5 0.92 999000 
7/21/2017 10:00 0.03 18 45 16 21 2.777778 157.5 0.92 999000 
7/21/2017 11:00 0.03 18 45 16 21 2.777778 157.5 0.92 999000 
7/21/2017 12:00 0.02 17 45 16 21 2.777778 135 0.94 999000 
7/21/2017 13:00 0.03 17 45 16 21 2.777778 135 0.95 999000 
7/21/2017 14:00 0.03 17 45 16 21 2.777778 135 0.98 999000 
7/21/2017 15:00 0.03 17 45 16 21 2.777778 157.5 0.97 999000 
7/21/2017 16:00 0.04 17 45 16 21 2.777778 135 0.97 999000 
7/21/2017 17:00 0.04 17 45 16 21 2.777778 135 0.98 999000 
7/21/2017 18:00 0.04 17 45 16 21 2.777778 135 1 999000 
7/21/2017 19:00 0.03 16 45 16 21 2.777778 135 1 999000 
7/21/2017 20:00 0.03 17 45 16 21 3.0555558 135 1 999000 
7/21/2017 21:00 0.03 17 45 16 21 3.0555558 135 1 999000 
7/21/2017 22:00 0.03 17 45 16 21 3.0555558 135 0.99 999000 
7/21/2017 23:00 0.03 17 45 16 21 3.0555558 157.5 0.97 999000 

我要計算爲col_a,col_b日常手段... col_h。 fid列似乎包含數字,但它們實際上是作爲字符串存儲的。對於那一欄,我只想每天的獨特字符串。然而,當我這樣做:

df.resample('D').mean() 

fid列disappers從最終的輸出。我如何在最終輸出中獲得它?

+0

'lambda'和'if esle' – Wen

回答

1

如果需要對某些值進行不同的重新採樣(如列fid,因爲文本列),可以使用Resampler.agg,dict,這可以動態創建。

最後添加reindex_axis獲得與輸入df相同的列順序。

#all columns without `fid` are aggregate by mean 
d = {x:'mean' for x in df.columns.difference(['fid'])} 
#added new item to dict - column fid is aggregate by first 
d['fid'] = 'first' 
print (d) 
{'col_e': 'mean', 'col_c': 'mean', 'col_b': 'mean', 'col_f': 'mean', 
'col_a': 'mean', 'col_d': 'mean', 'fid': 'first', 'col_h': 'mean', 'col_g': 'mean'} 


df1 = df.resample('D').agg(d).reindex_axis(df.columns, axis=1) 
print (df1) 
       col_a  col_b col_c col_d col_e  col_f  col_g \ 
2017-07-20 0.020000 17.500000  45  17  19 2.103175 162.321429 
2017-07-21 0.031667 18.333333  45  16  21 3.206019 147.187500 

       col_h  fid 
2017-07-20 0.967143 999000 
2017-07-21 0.921250 999000 

如果Resampler.mean重採樣只,所有非數字列已排除(similar as aggregation):

df1 = df.resample('D').mean() 
print (df1) 
       col_a  col_b col_c col_d col_e  col_f  col_g \ 
2017-07-20 0.020000 17.500000 45.0 17.0 19.0 2.103175 162.321429 
2017-07-21 0.031667 18.333333 45.0 16.0 21.0 3.206019 147.187500 

       col_h 
2017-07-20 0.967143 
2017-07-21 0.921250 

另一種解決方案是使用Grouper如果在fid數據是每天相同:

df1 = df.groupby(['fid', pd.Grouper(freq='D')]) 
     .mean() 
     .reset_index() 
     .reindex_axis(df.columns, axis=1) 
print (df1) 
     col_a  col_b col_c col_d col_e  col_f  col_g  col_h \ 
0 0.020000 17.500000 45.0 17.0 19.0 2.103175 162.321429 0.967143 
1 0.031667 18.333333 45.0 16.0 21.0 3.206019 147.187500 0.921250 

     fid 
0 999000 
1 999000 
+0

謝謝@jezrael,我想保留它作爲字符串,因爲它可能會字母數字下降t他路。 – user308827

+0

@ user308827在這種情況下,您可以使用新類型 – Jakub

+0

創建一個新列,但如果需要合併數據 - 例如從4行創建1,希望'fid'的輸出如何?第一個值? – jezrael