簡體   English   中英

如何根據 python 中另一列的條件查找兩個日期之間特定列的最大值

[英]How do I Find max value of a particular column between 2 dates based on a condition from another column in python

我能否就如何根據另一列的條件在兩個日期之間查找特定列的最大值獲得一些幫助。”

我有一個像下面這樣的 df,我需要在條件所在的行之間找到最大值 - ['mark'] 列中的 2'act' 值之間的 ['high'] 列的最大值相同的 ['symbol'] 並將值存儲在新列中。

即在 2021 年 4 月 3 日至 2021 年 9 月 3 日之間找到 APPLE 的最大值,因為這兩個日期在標記列中都有“行為”。 [ 欄目中標注了更多的“行為”,但由於篇幅限制,在這里分享一個簡短的版本]

2021 年 4 月 3 日至 2021 年 3 月 10 日之間的橙色也是如此。

它不應該在 09 上為 Apple 的“act”標記做這個計算,因為在那之后蘋果沒有更多的行為。

數據:

日期 象征 打開 高的 低的 標記
2021 年 3 月 3 日 蘋果 732 754.95 723.4 729.85
2021 年 4 月 3 日 蘋果 733.25 765.7 715.85 752.45 行為
2021 年 5 月 3 日 蘋果 752.45 761 730.5 748.95
2021 年 8 月 3 日 蘋果 762.7 767.8 744.2 748.4
2021 年 9 月 3 日 蘋果 755.55 759.4 738.65 750.75 行為
2021 年 10 月 3 日 蘋果 757.5 753.1 743 745.35
2021 年 12 月 3 日 蘋果 743 752.1 723 728.15
15/03/2021 蘋果 727.8 727.8 706.05 719.05
2021 年 3 月 3 日 2406 2417.7 2375.8 2402.1
2021 年 4 月 3 日 2380 2435 2350 2417.1 行為
2021 年 5 月 3 日 2399 2423.9 2377.1 2387.1
2021 年 8 月 3 日 2383 2413.5 2360.05 2382.7
2021 年 9 月 3 日 2400 2444 2396.15 2422.7
2021 年 10 月 3 日 2446 2446 2415.55 2431.95 行為
2021 年 12 月 3 日 2442.8 2464.65 2397 2401.35
15/03/2021 2402.55 2427.55 2343.05 2355

好的,我已經對此有所了解 - 首先我重新創建了 dataframe:

import pandas as pd

data={("03/03/2021","APPLE",732,754.95,723.4,729.85,), 
      ("04/03/2021","APPLE",733.25,765.7,715.85,752.45,"act"), 
      ("05/03/2021","APPLE",752.45,761,730.5,748.95,), 
      ("08/03/2021","APPLE",762.7,767.8,744.2,748.4,), 
      ("09/03/2021","APPLE",755.55,759.4,738.65,750.75,"act"), 
      ("10/03/2021","APPLE",757.5,753.1,743,745.35,), 
      ("12/03/2021","APPLE",743,752.1,723,728.15,), 
      ("15/03/2021","APPLE",727.8,727.8,706.05,719.05,), 
      ("03/03/2021","ORANGE",2406,2417.7,2375.8,2402.1,), 
      ("04/03/2021","ORANGE",2380,2435,2350,2417.1,"act"), 
      ("05/03/2021","ORANGE",2399,2423.9,2377.1,2387.1,), 
      ("08/03/2021","ORANGE",2383,2413.5,2360.05,2382.7,), 
      ("09/03/2021","ORANGE",2400,2444,2396.15,2422.7,), 
      ("10/03/2021","ORANGE",2446,2446,2415.55,2431.95,"act"), 
      ("12/03/2021","ORANGE",2442.8,2464.65,2397,2401.35,), 
      ("15/03/2021","ORANGE",2402.55,2427.55,2343.05,2355,)}

df = pd.DataFrame(data, 
                  columns=("date","symbol","open","high","low","close","mark")).
                  sort_values(by=["symbol", "date"]).fillna("").reset_index(drop=True)

我認為您想要做的是對 group-by 的簡單max 棘手的部分是操縱您的數據,使其符合 group-by 的預期。 也就是說,要分組的字段。

def block_diff(series, trigger, start_stop=False):
    toggle = False
    rs = list()
    for i,v in series.iteritems():
        if v==trigger:
            if start_stop and toggle:
                rs.append(toggle)
                toggle=not toggle
            elif start_stop and not toggle:
                toggle=not toggle
                rs.append(toggle)
            elif not start_stop:
                toggle=not toggle
                rs.append(toggle)
        else:
            rs.append(toggle)
    return pd.Series(rs)

the regions that are going to feature in the group-by.所以上面的 function 被定義了——這里的想法是我們想要的區域。 這個 function 接受一個系列、一些匹配的觸發值和一個 start_stop 標志來微調行為。

如果我將其應用於 dataframe,使用返回的True/False值作為索引來填充分組變量的副本並將結果存儲在一個名為act_block的新字段中,然后我創建一個唯一的分組字段,該字段也用作開始-停止過濾。 同時,我還創建了一個名為act_sequence的附加列,稍后我們將使用它來標識每個子組的初始起始行。

df['act_block'] = df[block_diff(df['mark'], "act", True)]['symbol']
df['act_sequence'] = df.groupby("act_block").cumcount()
df

    date        symbol  open    high    low     close   mark    act_block   act_sequence
0   03/03/2021  APPLE   732.00  754.95  723.40  729.85          NaN         0
1   04/03/2021  APPLE   733.25  765.70  715.85  752.45  act     APPLE       0
2   05/03/2021  APPLE   752.45  761.00  730.50  748.95          APPLE       1
3   08/03/2021  APPLE   762.70  767.80  744.20  748.40          APPLE       2
4   09/03/2021  APPLE   755.55  759.40  738.65  750.75  act     APPLE       3
5   10/03/2021  APPLE   757.50  753.10  743.00  745.35          NaN         1
6   12/03/2021  APPLE   743.00  752.10  723.00  728.15          NaN         2
7   15/03/2021  APPLE   727.80  727.80  706.05  719.05          NaN         3
8   03/03/2021  ORANGE  2406.00 2417.70 2375.80 2402.10         NaN         4
9   04/03/2021  ORANGE  2380.00 2435.00 2350.00 2417.10 act     ORANGE      0
10  05/03/2021  ORANGE  2399.00 2423.90 2377.10 2387.10         ORANGE      1
11  08/03/2021  ORANGE  2383.00 2413.50 2360.05 2382.70         ORANGE      2
12  09/03/2021  ORANGE  2400.00 2444.00 2396.15 2422.70         ORANGE      3
13  10/03/2021  ORANGE  2446.00 2446.00 2415.55 2431.95 act     ORANGE      4
14  12/03/2021  ORANGE  2442.80 2464.65 2397.00 2401.35         NaN         5
15  15/03/2021  ORANGE  2402.55 2427.55 2343.05 2355.00         NaN         6

現在我們可以在act_block上做一個簡單的 groupby,將結果保存到一個名為 max_groups 的系列中:

max_groups = df.groupby("act_block")["high"].max()


act_block
APPLE      767.8
ORANGE    2446.0
Name: high, dtype: float64

Take this series and merge it with the original dataframe - if we do this with a filter, the max_vals object will inherit the original dataframe's index, allowing us to do a pd.concat to selectively join the two objects together to produce the intended output.

max_vals = df.merge(max_groups, left_on=["act_block"], right_on="act_block",how="left")[(df['act_sequence']==0)].fillna("")['high_y']
max_vals.name="max_val"
new_df = pd.concat([df, max_vals], axis=1).fillna("")
new_df = new_df[["date","symbol","open","high","low","close","mark","max_val"]]

new_df
日期 象征 打開 高的 低的 標記 max_val
0 2021 年 3 月 3 日 蘋果 732.00 754.95 723.40 729.85
1 2021 年 4 月 3 日 蘋果 733.25 765.70 715.85 752.45 行為 767.8
2 2021 年 5 月 3 日 蘋果 752.45 761.00 730.50 748.95
3 2021 年 8 月 3 日 蘋果 762.70 767.80 744.20 748.40
4 2021 年 9 月 3 日 蘋果 755.55 759.40 738.65 750.75 行為
5 2021 年 10 月 3 日 蘋果 757.50 753.10 743.00 745.35
6 2021 年 12 月 3 日 蘋果 743.00 752.10 723.00 728.15
7 15/03/2021 蘋果 727.80 727.80 706.05 719.05
8 2021 年 3 月 3 日 2406.00 2417.70 2375.80 2402.10
9 2021 年 4 月 3 日 2380.00 2435.00 2350.00 2417.10 行為 2446
10 2021 年 5 月 3 日 2399.00 2423.90 2377.10 2387.10
11 2021 年 8 月 3 日 2383.00 2413.50 2360.05 2382.70
12 2021 年 9 月 3 日 2400.00 2444.00 2396.15 2422.70
13 2021 年 10 月 3 日 2446.00 2446.00 2415.55 2431.95 行為
14 2021 年 12 月 3 日 2442.80 2464.65 2397.00 2401.35
15 15/03/2021 2402.55 2427.55 2343.05 2355.00

暫無
暫無

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

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