簡體   English   中英

獲取pandas中2個相應系列的min和max元素

[英]Get min and max elements for 2 corresponding series in 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),max_func:a *(a> b)+ b *( b> = a))

謝謝

將系列組合成一個自動與索引對齊的框架

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM