【问题标题】:Convert float Series into an integer Series in pandas将浮点系列转换为熊猫中的整数系列
【发布时间】:2013-10-02 07:54:56
【问题描述】:

我有以下数据框:

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 的行。我这样做:

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() 不采用系列或列表作为参数? pandas 中有什么方法可以做到这一点吗?

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    尝试使用 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')
    
    ...
    

    【讨论】:

    • 使用astype() 绝对正确,但同时避免列表理解会更好。喜欢ts['time'] = (ts.time / 100).astype('int') 然后与ts.grouby('time') 分组等等...
    • 是的,同意,避免列表理解会更好,将编辑我的答案以反映。
    【解决方案2】:

    这是解决问题的另一种方法

    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
    

    按 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
    

    【讨论】:

    • 感谢@Jeff!这种方法看起来不错。有些方法对我来说是新的。我会试试这个。现在,我将使用@drexiya 给出的答案。
    • 感谢@Jeff。我在发表评论后找到了这个资源。所以我删除了提到相同的评论。
    【解决方案3】:

    这是将ts 转换为int 类型的Series 的另一种非常通用的方法:

    rise_p['ts'] = (rise_p.time / 100).apply(lambda val: int(val))
    

    apply 允许您按值将任意函数应用于您的Series 对象值。 apply 也适用于 DataFrame 对象的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      • 2019-10-12
      • 2014-11-23
      相关资源
      最近更新 更多