【问题标题】:Get min and max elements for 2 corresponding series in pandas获取熊猫中2个相应系列的最小和最大元素
【发布时间】:2014-04-28 20:50:43
【问题描述】:

假设我在 pandas 中有 2 个系列:

from datetime import datetime, timedelta
import pandas as pd
d = datetime.now()
index = [d + timedelta(seconds = i) for i in range(5)]
a = pd.Series([1,4,5,7,8], index = index)
b = pd.Series([2,3,6,7,8], index = index)

获取相应索引元素的最小值/最大值的最佳方法是什么。 喜欢:

min_func(a, b): [1,3,5,7,8] (for given index)
max_func(a, b): [2,4,6,7,8]

我可以在文档中找到的唯一函数是在系列中返回 min/max 的 min/max 函数,而 .apply 函数不采用 index 参数。 有没有更好的方法来实现它而无需手动系列迭代或一些算术魔术(如 min_func: a * (a b) + b * ( b >= a) )

谢谢

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    将系列组合成一个按索引自动对齐的框架

    In [51]: index
    Out[51]: 
    [datetime.datetime(2013, 8, 26, 18, 33, 48, 990974),
     datetime.datetime(2013, 8, 26, 18, 33, 49, 990974),
     datetime.datetime(2013, 8, 26, 18, 33, 50, 990974),
     datetime.datetime(2013, 8, 26, 18, 33, 51, 990974),
     datetime.datetime(2013, 8, 26, 18, 33, 52, 990974)]
    
    In [52]: a = pd.Series([1,4,5,7,8], index = index)
    
    In [53]: b = pd.Series([2,3,6,7,8], index = index)
    
    In [54]: a
    Out[54]: 
    2013-08-26 18:33:48.990974    1
    2013-08-26 18:33:49.990974    4
    2013-08-26 18:33:50.990974    5
    2013-08-26 18:33:51.990974    7
    2013-08-26 18:33:52.990974    8
    dtype: int64
    
    In [55]: b
    Out[55]: 
    2013-08-26 18:33:48.990974    2
    2013-08-26 18:33:49.990974    3
    2013-08-26 18:33:50.990974    6
    2013-08-26 18:33:51.990974    7
    2013-08-26 18:33:52.990974    8
    dtype: int64
    
    In [56]: df = DataFrame({ 'a' : a, 'b' : b })
    
    In [57]: df
    Out[57]: 
                                a  b
    2013-08-26 18:33:48.990974  1  2
    2013-08-26 18:33:49.990974  4  3
    2013-08-26 18:33:50.990974  5  6
    2013-08-26 18:33:51.990974  7  7
    2013-08-26 18:33:52.990974  8  8
    

    最小/最大

    In [9]: df.max(1)
    Out[9]: 
    2013-08-26 18:33:48.990974    2
    2013-08-26 18:33:49.990974    4
    2013-08-26 18:33:50.990974    6
    2013-08-26 18:33:51.990974    7
    2013-08-26 18:33:52.990974    8
    Freq: S, dtype: int64
    
    In [10]: df.min(1)
    Out[10]: 
    2013-08-26 18:33:48.990974    1
    2013-08-26 18:33:49.990974    3
    2013-08-26 18:33:50.990974    5
    2013-08-26 18:33:51.990974    7
    2013-08-26 18:33:52.990974    8
    Freq: S, dtype: int64
    

    最小值/最大值索引

    In [11]: df.idxmax(1)
    Out[11]: 
    2013-08-26 18:33:48.990974    b
    2013-08-26 18:33:49.990974    a
    2013-08-26 18:33:50.990974    b
    2013-08-26 18:33:51.990974    a
    2013-08-26 18:33:52.990974    a
    Freq: S, dtype: object
    
    In [12]: df.idxmin(1)
    Out[12]: 
    2013-08-26 18:33:48.990974    a
    2013-08-26 18:33:49.990974    b
    2013-08-26 18:33:50.990974    a
    2013-08-26 18:33:51.990974    a
    2013-08-26 18:33:52.990974    a
    Freq: S, dtype: object
    

    【讨论】:

    • 看原帖想要的输出,我想你要加axis=1。
    • 感谢您的详细回复。与算术解决方案相比,性能影响是什么?基于 DataFrame 的内部结构,创建新的数据帧是否比一些加法/减法更昂贵?
    • 这都是矢量化的;创建新框架通常比修改现有框架要快得多; pandas 的语义是为此而设计的,很少有操作修改(尽管有些可以使用 inplace 关键字)
    • 我想找到 15 分钟时间范围内的最大值和最小值以及如何做。?
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2020-04-28
    • 1970-01-01
    • 2020-02-19
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多