2013-09-26 187 views
8

的整數系列I有如下的數據幀:轉換浮子系列到大熊貓

In [31]: rise_p 
Out[31]: 
     time magnitude 
0 1379945444 156.627598 
1 1379945447 1474.648726 
2 1379945448 1477.448999 
3 1379945449 1474.886202 
4 1379945699 1371.454224 

現在,我想組行,其在一分鐘之內。所以我把時間序列與100分開。我得到這個:

In [32]: rise_p/100 
Out[32]: 
      time magnitude 
0 13799454.44 1.566276 
1 13799454.47 14.746487 
2 13799454.48 14.774490 
3 13799454.49 14.748862 
4 13799456.99 13.714542 

如上所述,我想創建基於時間的組。因此,預期的子組將是時間爲1379945413799456的行。我這樣做:

​​

如何轉換ts爲整數系列自INT()不採取系列或列表作爲參數?熊貓有什麼方法可以做到這一點嗎?

回答

13

嘗試用astype轉換:

new_re_df = [s.iloc[np.where(ts.astype(int) == int(i))] for i in ts] 

編輯

論@Rutger Kassies建議一個更好的辦法是投系列,然後GROUPBY:

rise_p['ts'] = (rise_p.time/100).astype('int') 

ts_grouped = rise_p.groupby('ts') 

... 
+3

使用'astype()'是絕對正確的,但是避免列表理解會更好。像'ts ['time'] =(ts.time/100).astype('int')',然後用'ts.grouby('time')'進行分組等等...... –

+0

Yes agree,avoid the列表理解會更好,將編輯我的答案來反映。 – drexiya

4

這裏有一個不同的方式來解決你的問題

In [3]: df 
Out[3]: 
     time magnitude 
0 1379945444 156.627598 
1 1379945447 1474.648726 
2 1379945448 1477.448999 
3 1379945449 1474.886202 
4 1379945699 1371.454224 

In [4]: df.dtypes 
Out[4]: 
time   int64 
magnitude float64 
dtype: object 

將您的紀元時間戳秒

In [7]: df['time'] = pd.to_datetime(df['time'],unit='s') 

設置索引

In [8]: df.set_index('time',inplace=True) 

In [9]: df 
Out[9]: 
         magnitude 
time        
2013-09-23 14:10:44 156.627598 
2013-09-23 14:10:47 1474.648726 
2013-09-23 14:10:48 1477.448999 
2013-09-23 14:10:49 1474.886202 
2013-09-23 14:14:59 1371.454224 

GROUPBY 1分鐘,平均結果(how=可以是任意功能以及)

In [10]: df.resample('1Min',how=np.mean) 
Out[10]: 
         magnitude 
time        
2013-09-23 14:10:00 1145.902881 
2013-09-23 14:11:00   NaN 
2013-09-23 14:12:00   NaN 
2013-09-23 14:13:00   NaN 
2013-09-23 14:14:00 1371.454224 
+0

Thanx @Jeff!這種方法看起來不錯。有些方法對我來說是新的。我會試試這個。現在,我將使用@drexiya給出的答案。 – Geekster

+0

http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-zone-handling – Jeff

+0

Thanx @Jeff。發佈評論後我發現了這個資源。所以我刪除了相同的評論。 – Geekster