I have the following data frame:
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
如上所述,我希望根据时间创建组。因此预期的子组将是具有时间 13799454
和 13799456
的行。我这样做:
In [37]: ts = rise_p['time']/100
In [38]: s = rise_p/100
In [39]: new_re_df = [s.iloc[np.where(int(ts) == int(i))] for i in ts]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-39-5ea498cf32b2> in <module>()
----> 1 new_re_df = [s.iloc[np.where(int(ts) == int(i))] for i in ts]
TypeError: only length-1 arrays can be converted to Python scalars
如何将ts
转换为整数系列,因为int()不接受Series或list作为参数?Pandas中是否有任何方法可以实现此操作?
astype()
是正确的,但更好的做法是完全避免使用列表推导式。例如ts['time'] = (ts.time / 100).astype('int')
,然后使用ts.groupby('time')
进行分组等操作... - Rutger Kassies