我将这些列“价格”、“功能”和“猫”作为 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
我想再添加 2 个列“min”和“max”。 min 将计算从前 4 或 7 行到当前 4 或 7 的价格 col 的最小值。同样,max col 将计算从前 4 或 7 行到当前 4 或 7 行的价格 col 的最大值7、需具备以下条件。 1.min和max的值只在函数有4或7的行计算。 2. 仅当 4 出现在 7 之后或 7 出现在 7 之后时才计算这些值。即,如果 4 出现在前 4 之后,则不计算最小值和最大值,如果 7 出现在前 7 之后,则同样的方式计算最小值& 不计算最大值。例如如果当前行函数为 7,则最小值和最大值将从前 4 行计算得出,如果前一行为 7,则最小值和最大值应为空。 3. 最小值和最大值也将在最后一行中根据前 4 或 7 进行计算。 最后表格应该是这样的。
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
我正在使用以下代码,但是此代码在最后 4 或 7 直到最后一行之后重复相同的 min 和 max 值。
# 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()
假设您想要排除之前的 4-7 的最小/最大值,这是一个简单的
groupby.transform
:
group = df.loc[::-1, 'cat'].eq('4-7').cumsum()[::-1]
t = df.groupby(group)['Price'].transform
df['min'] = t('min')
df['max'] = t('max')
输出(第 30-50 行):
Price functions cat min max
30 50925.75 3 3-8 50892.30 51272.85
31 50892.30 4 4-7 50892.30 51272.85
32 50880.00 3 3-8 50876.45 50941.95
33 50876.45 3 3-8 50876.45 50941.95
34 50901.00 8 3-8 50876.45 50941.95
35 50910.00 1 1-6 50876.45 50941.95
36 50941.95 7 4-7 50876.45 50941.95
37 50919.85 3 3-8 50898.40 51030.00
38 50912.45 3 3-8 50898.40 51030.00
39 50898.40 6 1-6 50898.40 51030.00
40 50930.00 1 1-6 50898.40 51030.00
41 50958.00 2 2-5 50898.40 51030.00
42 50986.15 1 1-6 50898.40 51030.00
43 50990.00 8 3-8 50898.40 51030.00
44 50994.00 8 3-8 50898.40 51030.00
45 51029.00 8 3-8 50898.40 51030.00
46 51030.00 8 3-8 50898.40 51030.00
47 51015.65 5 2-5 50898.40 51030.00
48 50997.15 3 3-8 50898.40 51030.00
49 50970.00 4 4-7 50898.40 51030.00
50 51002.55 1 1-6 51002.55 51511.40
groupby.agg
进行聚合,计算最小值/最大值/第一个,并使用下一组的第一个值修正最小值/最大值:
m = df['cat'].eq('4-7')
group = m.cumsum()
g = df.groupby(group)['Price'].agg
f = g('first').shift(-1)
df['min'] = group.map(np.fmin(g('min'), f))
df['max'] = group.map(np.fmax(g('max'), f))
输出:
Price functions cat min max
30 50925.75 3 3-8 50892.30 51272.85
31 50892.30 4 4-7 50876.45 50941.95
32 50880.00 3 3-8 50876.45 50941.95
33 50876.45 3 3-8 50876.45 50941.95
34 50901.00 8 3-8 50876.45 50941.95
35 50910.00 1 1-6 50876.45 50941.95
36 50941.95 7 4-7 50898.40 51030.00
37 50919.85 3 3-8 50898.40 51030.00
38 50912.45 3 3-8 50898.40 51030.00
39 50898.40 6 1-6 50898.40 51030.00
40 50930.00 1 1-6 50898.40 51030.00
41 50958.00 2 2-5 50898.40 51030.00
42 50986.15 1 1-6 50898.40 51030.00
43 50990.00 8 3-8 50898.40 51030.00
44 50994.00 8 3-8 50898.40 51030.00
45 51029.00 8 3-8 50898.40 51030.00
46 51030.00 8 3-8 50898.40 51030.00
47 51015.65 5 2-5 50898.40 51030.00
48 50997.15 3 3-8 50898.40 51030.00
49 50970.00 4 4-7 50970.00 51511.40
50 51002.55 1 1-6 50970.00 51511.40