[Python] pandas get min and max value for multiple rows

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    i have these cols "Price", "functions", & "cat" as df.

    Price functions cat
    51272.85 8 3-8
    51134.15 3 3-8
    51150 8 3-8
    51161.3 1 1-6
    51165.45 1 1-6
    51138.65 3 3-8
    51060 3 3-8
    51095 1 1-6
    51099.5 3 3-8
    51093.65 8 3-8
    51108 1 1-6
    51107.5 1 1-6
    51112.65 6 1-6
    51106.9 3 3-8
    51096.9 3 3-8
    51110 1 1-6
    51104 1 1-6
    51041.4 3 3-8
    50990 3 3-8
    50974.35 3 3-8
    50972.95 3 3-8
    50981.7 8 3-8
    50989.95 1 1-6
    51002.65 2 2-5
    51009.45 1 1-6
    50963.15 3 3-8
    50972.55 8 3-8
    50910.6 3 3-8
    50930 1 1-6
    50965 2 2-5
    50925.75 3 3-8
    50892.3 4 4-7
    50880 3 3-8
    50876.45 3 3-8
    50901 8 3-8
    50910 1 1-6
    50941.95 7 4-7
    50919.85 3 3-8
    50912.45 3 3-8
    50898.4 6 1-6
    50930 1 1-6
    50958 2 2-5
    50986.15 1 1-6
    50990 8 3-8
    50994 8 3-8
    51029 8 3-8
    51030 8 3-8
    51015.65 5 2-5
    50997.15 3 3-8
    50970 4 4-7
    51002.55 1 1-6
    51021.1 8 3-8
    51038.15 2 2-5
    51034.6 1 1-6
    51070 8 3-8
    51079.9 1 1-6
    51140 8 3-8
    51160.05 8 3-8
    51174.95 1 1-6
    51180 6 1-6
    51148.35 6 1-6
    51182 2 2-5
    51170.05 6 1-6
    51160.35 3 3-8
    51172.15 1 1-6
    51180 1 1-6
    51240 8 3-8
    51240 8 3-8
    51235 3 3-8
    51235 6 1-6
    51255 8 3-8
    51221 3 3-8
    51230.45 1 1-6
    51235 8 3-8
    51258.25 8 3-8
    51245.95 3 3-8
    51253 1 1-6
    51224.2 3 3-8
    51229.85 1 1-6
    51216 3 3-8
    51192.95 3 3-8
    51189.9 3 3-8
    51199 8 3-8
    51210.45 2 2-5
    51240 8 3-8
    51227.1 3 3-8
    51217.15 3 3-8
    51223 8 3-8
    51225 8 3-8
    51246.05 8 3-8
    51254.35 8 3-8
    51264.75 1 1-6
    51316.15 8 3-8
    51355.8 1 1-6
    51425 8 3-8
    51511.4 7 4-7
    51490.25 6 1-6
    51508.5 1 1-6
    51515 7 4-7
    51506.1 4 4-7
    51513.95 8 3-8
    51529.05 8 3-8
    51515.1 6 1-6
    51525 1 1-6
    51500 6 1-6
    51511.65 7 4-7
    51529.9 1 1-6
    51472.2 3 3-8
    51475.05 8 3-8
    51460 3 3-8
    51447.85 3 3-8
    51470 1 1-6
    51479.4 8 3-8
    51485.5 2 2-5
    51461.35 6 1-6
    51441.8 3 3-8
    51435 3 3-8
    51435.7 1 1-6
    51440.5 8 3-8
    51448.6 8 3-8
    51446.5 3 3-8
    51440.15 3 3-8
    51510 8 3-8
    51520.95 6 1-6
    51502 3 3-8
    51504 3 3-8
    51470 3 3-8
    51479.9 8 3-8
    51455 3 3-8
    51447.6 3 3-8
    51439.7 6 1-6
    51453.8 1 1-6
    51447 3 3-8
    51380.35 3 3-8
    51385.35 1 1-6
    51394 8 3-8
    51385 3 3-8
    51397.05 8 3-8
    51392.7 3 3-8
    51329.4 3 3-8
    51340.25 8 3-8
    51347.25 8 3-8
    51365.35 1 1-6
    51332.95 3 3-8
    51337.75 6 1-6
    51338.9 1 1-6
    51370 8 3-8
    51364.85 6 1-6
    51379.6 1 1-6
    51385 8 3-8
    51380 6 1-6
    51415 8 3-8
    51404.05 6 1-6
    51390.8 3 3-8
    51390.15 3 3-8
    51390 3 3-8
    51389.5 3 3-8
    51384 6 1-6
    51390.6 8 3-8
    51360 3 3-8
    51339.3 3 3-8
    51345 6 1-6
    51365.6 1 1-6
    51340 3 3-8
    51335 3 3-8
    51346.7 8 3-8
    51369.95 2 2-5
    51369.95 3 3-8
    51392.1 7 4-7
    51420 7 4-7
    51415 3 3-8
    51418 8 3-8
    51411.15 3 3-8
    51448.2 2 2-5
    51439.3 6 1-6
    51430 6 1-6
    51430 8 3-8
    51439.65 6 1-6
    51430.1 8 3-8
    51443.55 8 3-8
    51470 8 3-8
    51485 8 3-8
    51487.3 8 3-8
    51475 3 3-8
    51485 7 4-7
    51519.05 8 3-8
    51520.55 8 3-8
    51507.5 3 3-8
    51492.9 3 3-8
    51484.15 3 3-8
    51495.5 8 3-8
    51479 3 3-8
    51480 8 3-8
    51470 3 3-8
    51460.85 6 1-6
    51480 8 3-8
    51488.15 1 1-6
    51470.2 5 2-5
    51453 3 3-8
    51455.05 7 4-7
    51455 3 3-8
    51450 3 3-8
    51440 6 1-6
    51440 1 1-6
    51437.75 6 1-6
    51454.6 1 1-6
    51425 3 3-8
    51420 3 3-8
    51427.9 1 1-6
    51428.95 3 3-8
    51452.65 8 3-8
    51440.9 3 3-8
    51450.1 8 3-8
    51464.8 8 3-8
    51450 3 3-8
    51430 5 2-5
    51442 8 3-8
    51450 8 3-8
    51434.45 3 3-8
    51421 3 3-8
    51404 3 3-8
    51397.1 3 3-8
    51390.15 3 3-8
    51397.25 8 3-8
    51381.55 3 3-8
    51394.95 1 1-6
    51395 8 3-8
    51380 6 1-6
    51390.7 7 4-7
    51386.55 3 3-8
    51394.95 8 3-8
    51391.65 6 1-6
    51399.2 2 2-5
    51399
    51390 3 3-8
    51390 1 1-6
    51355.05 5 2-5
    51369.95 1 1-6
    51370 1 1-6
    51359 4 4-7
    51388.1 8 3-8
    51391.9 8 3-8
    51391.75 3 3-8
    51394.8 8 3-8
    51399.9 7 4-7
    51421 8 3-8
    51440.35 8 3-8
    51425 6 1-6
    51443.9 8 3-8
    51445.4 8 3-8
    51440.9 3 3-8
    51426 4 4-7
    51424.9 3 3-8
    51435
    51449.5 3 3-8
    51450 6 1-6
    51449 6 1-6
    51465.95 8 3-8
    51436.6 3 3-8
    51440.55 8 3-8
    51425 4 4-7
    51401.75 3 3-8
    51409.25 8 3-8
    51460 8 3-8
    51470 8 3-8
    51483.65 8 3-8
    51469.2 6 1-6
    51480 8 3-8
    51461.65 6 1-6
    51474 8 3-8
    51484.95 8 3-8
    51478.4 4 4-7
    51478.05 3 3-8
    51486.2 8 3-8
    51462.1 3 3-8
    51470.15 1 1-6
    51470 6 1-6
    51485 8 3-8
    51548 8 3-8
    51545 3 3-8
    51536.3 1 1-6
    51571.3 8 3-8
    51560 3 3-8
    51559 6 1-6
    51620 8 3-8
    51639.6 8 3-8
    51644 8 3-8
    51654.4 8 3-8
    51654.45 8 3-8
    51630.7 3 3-8
    51632.4 8 3-8
    51640 8 3-8
    51644.7 8 3-8
    51637.95 6 1-6
    51610.65 3 3-8
    51595 3 3-8
    51576 6 1-6
    51556.7 3 3-8
    51551.15 3 3-8
    51584.95 1 1-6
    51577.05 1 1-6
    51554.25 5 2-5
    51569 1 1-6
    51543.85 3 3-8
    51560 8 3-8
    51564.75 8 3-8
    51560 3 3-8
    51574 8 3-8
    51570 3 3-8
    51590 8 3-8
    51573.2 3 3-8
    51573.4 1 1-6
    51538.35 3 3-8
    51550 8 3-8
    51549.9 6 1-6
    51549.4 6 1-6
    51542.75 3 3-8
    51535 6 1-6
    51530 3 3-8
    51520 3 3-8
    51540 1 1-6
    51565 2 2-5
    51542.2 3 3-8
    51547.35 1 1-6
    51545.4 3 3-8
    51534.05 3 3-8
    51530 3 3-8
    51534 1 1-6
    51525 3 3-8
    51490 3 3-8
    51470.6 3 3-8
    51464.85 3 3-8
    51435 3 3-8
    51451 8 3-8
    51456.35 7 4-7
    51464 8 3-8
    51460 3 3-8
    51473.15 8 3-8
    51471.75 3 3-8
    51440.2 5 2-5
    51426 3 3-8
    51429.95 1 1-6
    51435 2 2-5
    51438.65 1 1-6
    51412.6 3 3-8
    51426.25 1 1-6
    51366 3 3-8
    51395 2 2-5
    51410 8 3-8
    51419.1 8 3-8
    51408.85 6 1-6
    51400 3 3-8
    51398.8 3 3-8
    51402.5 1 1-6
    51413.5 8 3-8
    51416.35 1 1-6
    51422.4 8 3-8
    51445 8 3-8
    51440 3 3-8
    51428.4 4 4-7
    51437.15 1 1-6
    51430.75 3 3-8
    51440 8 3-8
    51430.5 6 1-6
    51442.05 7 4-7
    51436.25 8 3-8
    51474.95 8 3-8
    51478.25 1 1-6
    51481.35 8 3-8
    51470.05 5 2-5
    51469.95 3 3-8
    51465.7 3 3-8
    51470.7 8 3-8
    51486.75 1 1-6
    51493.65 Last


    i want to add 2 more cols "min" & "max". min will calculate the minimum value of Price col from the row of previous 4 or 7 of till current 4 or 7. same way max col will calculate the maximum value of price col from the row of previous 4 or 7 till current row of 4 or 7. Follwoing conditions needs to be there. 1.The value of min & max only to be calculated in the row where functions has 4 or 7. 2. These value to be calculated only if 4 is appearing after 7 or 7 is appearing after 7. i.e. if 4 is appearing after previous 4 the min & max value not to be calculated, same way if 7 is appearing after pervious 7 the min & max value not to be calculated. e.g. if current row functions ahs 7 then min & max to be calculated from previous 4 and if previous is 7 then the min & max should be blank. 3. min & max to be calculated in last row as well from the previous 4 or 7. finally the table should look like this.

    Price functions cat min max
    51272.85 8 3-8
    51134.15 3 3-8
    51150 8 3-8
    51161.3 1 1-6
    51165.45 1 1-6
    51138.65 3 3-8
    51060 3 3-8
    51095 1 1-6
    51099.5 3 3-8
    51093.65 8 3-8
    51108 1 1-6
    51107.5 1 1-6
    51112.65 6 1-6
    51106.9 3 3-8
    51096.9 3 3-8
    51110 1 1-6
    51104 1 1-6
    51041.4 3 3-8
    50990 3 3-8
    50974.35 3 3-8
    50972.95 3 3-8
    50981.7 8 3-8
    50989.95 1 1-6
    51002.65 2 2-5
    51009.45 1 1-6
    50963.15 3 3-8
    50972.55 8 3-8
    50910.6 3 3-8
    50930 1 1-6
    50965 2 2-5
    50925.75 3 3-8
    50892.3 4 4-7 0 0
    50880 3 3-8
    50876.45 3 3-8
    50901 8 3-8
    50910 1 1-6
    50941.95 7 4-7 50876.45 50941.95
    50919.85 3 3-8
    50912.45 3 3-8
    50898.4 6 1-6
    50930 1 1-6
    50958 2 2-5
    50986.15 1 1-6
    50990 8 3-8
    50994 8 3-8
    51029 8 3-8
    51030 8 3-8
    51015.65 5 2-5
    50997.15 3 3-8
    50970 4 4-7 50898.4 51030
    51002.55 1 1-6
    51021.1 8 3-8
    51038.15 2 2-5
    51034.6 1 1-6
    51070 8 3-8
    51079.9 1 1-6
    51140 8 3-8
    51160.05 8 3-8
    51174.95 1 1-6
    51180 6 1-6
    51148.35 6 1-6
    51182 2 2-5
    51170.05 6 1-6
    51160.35 3 3-8
    51172.15 1 1-6
    51180 1 1-6
    51240 8 3-8
    51240 8 3-8
    51235 3 3-8
    51235 6 1-6
    51255 8 3-8
    51221 3 3-8
    51230.45 1 1-6
    51235 8 3-8
    51258.25 8 3-8
    51245.95 3 3-8
    51253 1 1-6
    51224.2 3 3-8
    51229.85 1 1-6
    51216 3 3-8
    51192.95 3 3-8
    51189.9 3 3-8
    51199 8 3-8
    51210.45 2 2-5
    51240 8 3-8
    51227.1 3 3-8
    51217.15 3 3-8
    51223 8 3-8
    51225 8 3-8
    51246.05 8 3-8
    51254.35 8 3-8
    51264.75 1 1-6
    51316.15 8 3-8
    51355.8 1 1-6
    51425 8 3-8
    51511.4 7 4-7 50970 51511.4
    51490.25 6 1-6
    51508.5 1 1-6
    51515 7 4-7
    51506.1 4 4-7 51490.25 51515
    51513.95 8 3-8
    51529.05 8 3-8
    51515.1 6 1-6
    51525 1 1-6
    51500 6 1-6
    51511.65 7 4-7 51500 51529.05
    51529.9 1 1-6
    51472.2 3 3-8
    51475.05 8 3-8
    51460 3 3-8
    51447.85 3 3-8
    51470 1 1-6
    51479.4 8 3-8
    51485.5 2 2-5
    51461.35 6 1-6
    51441.8 3 3-8
    51435 3 3-8
    51435.7 1 1-6
    51440.5 8 3-8
    51448.6 8 3-8
    51446.5 3 3-8
    51440.15 3 3-8
    51510 8 3-8
    51520.95 6 1-6
    51502 3 3-8
    51504 3 3-8
    51470 3 3-8
    51479.9 8 3-8
    51455 3 3-8
    51447.6 3 3-8
    51439.7 6 1-6
    51453.8 1 1-6
    51447 3 3-8
    51380.35 3 3-8
    51385.35 1 1-6
    51394 8 3-8
    51385 3 3-8
    51397.05 8 3-8
    51392.7 3 3-8
    51329.4 3 3-8
    51340.25 8 3-8
    51347.25 8 3-8
    51365.35 1 1-6
    51332.95 3 3-8
    51337.75 6 1-6
    51338.9 1 1-6
    51370 8 3-8
    51364.85 6 1-6
    51379.6 1 1-6
    51385 8 3-8
    51380 6 1-6
    51415 8 3-8
    51404.05 6 1-6
    51390.8 3 3-8
    51390.15 3 3-8
    51390 3 3-8
    51389.5 3 3-8
    51384 6 1-6
    51390.6 8 3-8
    51360 3 3-8
    51339.3 3 3-8
    51345 6 1-6
    51365.6 1 1-6
    51340 3 3-8
    51335 3 3-8
    51346.7 8 3-8
    51369.95 2 2-5
    51369.95 3 3-8
    51392.1 7 4-7
    51420 7 4-7
    51415 3 3-8
    51418 8 3-8
    51411.15 3 3-8
    51448.2 2 2-5
    51439.3 6 1-6
    51430 6 1-6
    51430 8 3-8
    51439.65 6 1-6
    51430.1 8 3-8
    51443.55 8 3-8
    51470 8 3-8
    51485 8 3-8
    51487.3 8 3-8
    51475 3 3-8
    51485 7 4-7
    51519.05 8 3-8
    51520.55 8 3-8
    51507.5 3 3-8
    51492.9 3 3-8
    51484.15 3 3-8
    51495.5 8 3-8
    51479 3 3-8
    51480 8 3-8
    51470 3 3-8
    51460.85 6 1-6
    51480 8 3-8
    51488.15 1 1-6
    51470.2 5 2-5
    51453 3 3-8
    51455.05 7 4-7
    51455 3 3-8
    51450 3 3-8
    51440 6 1-6
    51440 1 1-6
    51437.75 6 1-6
    51454.6 1 1-6
    51425 3 3-8
    51420 3 3-8
    51427.9 1 1-6
    51428.95 3 3-8
    51452.65 8 3-8
    51440.9 3 3-8
    51450.1 8 3-8
    51464.8 8 3-8
    51450 3 3-8
    51430 5 2-5
    51442 8 3-8
    51450 8 3-8
    51434.45 3 3-8
    51421 3 3-8
    51404 3 3-8
    51397.1 3 3-8
    51390.15 3 3-8
    51397.25 8 3-8
    51381.55 3 3-8
    51394.95 1 1-6
    51395 8 3-8
    51380 6 1-6
    51390.7 7 4-7
    51386.55 3 3-8
    51394.95 8 3-8
    51391.65 6 1-6
    51399.2 2 2-5
    51399
    51390 3 3-8
    51390 1 1-6
    51355.05 5 2-5
    51369.95 1 1-6
    51370 1 1-6
    51359 4 4-7 51329.4 51529.9
    51388.1 8 3-8
    51391.9 8 3-8
    51391.75 3 3-8
    51394.8 8 3-8
    51399.9 7 4-7 51359 51399.9
    51421 8 3-8
    51440.35 8 3-8
    51425 6 1-6
    51443.9 8 3-8
    51445.4 8 3-8
    51440.9 3 3-8
    51426 4 4-7 51399.9 51445.4
    51424.9 3 3-8
    51435
    51449.5 3 3-8
    51450 6 1-6
    51449 6 1-6
    51465.95 8 3-8
    51436.6 3 3-8
    51440.55 8 3-8
    51425 4 4-7
    51401.75 3 3-8
    51409.25 8 3-8
    51460 8 3-8
    51470 8 3-8
    51483.65 8 3-8
    51469.2 6 1-6
    51480 8 3-8
    51461.65 6 1-6
    51474 8 3-8
    51484.95 8 3-8
    51478.4 4 4-7
    51478.05 3 3-8
    51486.2 8 3-8
    51462.1 3 3-8
    51470.15 1 1-6
    51470 6 1-6
    51485 8 3-8
    51548 8 3-8
    51545 3 3-8
    51536.3 1 1-6
    51571.3 8 3-8
    51560 3 3-8
    51559 6 1-6
    51620 8 3-8
    51639.6 8 3-8
    51644 8 3-8
    51654.4 8 3-8
    51654.45 8 3-8
    51630.7 3 3-8
    51632.4 8 3-8
    51640 8 3-8
    51644.7 8 3-8
    51637.95 6 1-6
    51610.65 3 3-8
    51595 3 3-8
    51576 6 1-6
    51556.7 3 3-8
    51551.15 3 3-8
    51584.95 1 1-6
    51577.05 1 1-6
    51554.25 5 2-5
    51569 1 1-6
    51543.85 3 3-8
    51560 8 3-8
    51564.75 8 3-8
    51560 3 3-8
    51574 8 3-8
    51570 3 3-8
    51590 8 3-8
    51573.2 3 3-8
    51573.4 1 1-6
    51538.35 3 3-8
    51550 8 3-8
    51549.9 6 1-6
    51549.4 6 1-6
    51542.75 3 3-8
    51535 6 1-6
    51530 3 3-8
    51520 3 3-8
    51540 1 1-6
    51565 2 2-5
    51542.2 3 3-8
    51547.35 1 1-6
    51545.4 3 3-8
    51534.05 3 3-8
    51530 3 3-8
    51534 1 1-6
    51525 3 3-8
    51490 3 3-8
    51470.6 3 3-8
    51464.85 3 3-8
    51435 3 3-8
    51451 8 3-8
    51456.35 7 4-7 51401.75 51654.45
    51464 8 3-8
    51460 3 3-8
    51473.15 8 3-8
    51471.75 3 3-8
    51440.2 5 2-5
    51426 3 3-8
    51429.95 1 1-6
    51435 2 2-5
    51438.65 1 1-6
    51412.6 3 3-8
    51426.25 1 1-6
    51366 3 3-8
    51395 2 2-5
    51410 8 3-8
    51419.1 8 3-8
    51408.85 6 1-6
    51400 3 3-8
    51398.8 3 3-8
    51402.5 1 1-6
    51413.5 8 3-8
    51416.35 1 1-6
    51422.4 8 3-8
    51445 8 3-8
    51440 3 3-8
    51428.4 4 4-7 51366 51473.15
    51437.15 1 1-6
    51430.75 3 3-8
    51440 8 3-8
    51430.5 6 1-6
    51442.05 7 4-7 51428.4 51442.05
    51436.25 8 3-8
    51474.95 8 3-8
    51478.25 1 1-6
    51481.35 8 3-8
    51470.05 5 2-5
    51469.95 3 3-8
    51465.7 3 3-8
    51470.7 8 3-8
    51486.75 1 1-6
    51493.65 Last 51436.25 51493.65


    I am using following code however the this code is repeating the same values of min & max after the last 4 or 7 till the last row.

    # Initialize variables
    last_index = None
    last_seen = None

    # Initialize new columns
    pivot_table_reset['4-7'] = None
    pivot_table_reset['min'] = None
    pivot_table_reset['max'] = None

    # Iterate through the DataFrame
    for i, row in pivot_table_reset.iterrows():
    if row['functions'] in [4, 7]:
    # Update the '4-7' column with the current Price
    pivot_table_reset.at[i, '4-7'] = row['Price']

    # Calculate min and max from the last seen function (4 or 7) to the current row
    if last_index is not None:
    min_price = pivot_table_reset.loc[last_index:i, 'Price'].min()
    max_price = pivot_table_reset.loc[last_index:i, 'Price'].max()
    else:
    min_price = row['Price']
    max_price = row['Price']

    # Update the min and max columns
    pivot_table_reset.at[i, 'min'] = min_price
    pivot_table_reset.at[i, 'max'] = max_price

    # Update the last seen function and index
    last_index = i
    last_seen = row['functions']

    # Check if there are any remaining rows to handle (e.g., last row edge case)
    if last_index is not None:
    pivot_table_reset.loc[last_index:, 'min'] = pivot_table_reset.loc[last_index:, 'Price'].min()
    pivot_table_reset.loc[last_index:, 'max'] = pivot_table_reset.loc[last_index:, 'Price'].max()

    Continue reading...

Compartilhe esta Página