gpt4 book ai didi

python - 存储单个函数的迭代结果

转载 作者:行者123 更新时间:2023-12-01 04:26:38 24 4
gpt4 key购买 nike

我试图将同一函数的所有 30 次迭代存储在同一个数据框中。由于某种原因,我的数据框仅包含一次迭代。我需要对“结果”输出进行迭代吗?我的代码是:

df1 = pd.DataFrame.from_csv(filepath, index_col = None)

def find_peaks(x_data, y_data):
y_data = np.where(y_data > 5000, y_data, 0.1)
grad = np.diff(y_data)
peaks=[]
i = 0
while i < len(grad[:-1]):
if grad[i] > 0:
start = i
peak_index = find_peak(start, grad)
end = find_end(peak_index, grad)
area = np.trapz(y_data[start:end], x_data[start:end])
peaks.append((x_data[peak_index], y_data[peak_index], area))
i = end - 1
else:
i+=1
return peaks

for i in range(1,31):
result = find_peaks(df1['R'], df1['I {}'.format(i)])

df2 = pd.DataFrame(result)

print df2

输出:

        0          1             2
0 3053.6 105000.0 -5217775.735
1 3015.9 81892.0 -4013311.400
2 2962.8 98694.0 -2050799.050
3 2936.2 67884.0 -1140645.600
4 2906.3 2530000.0 -22099575.600
5 2871.5 102000.0 -653778.650
6 2777.9 8482.4 -68580.440
7 2719.3 11768.0 -91285.610
8 2625.0 5902.5 -40623.500
9 2599.3 5304.7 -69163.680
10 2573.5 18009.0 -170745.690
11 1538.0 12694.0 -965128.025
12 1467.7 9279.2 -144139.995
13 1451.4 21626.0 -280386.495
14 1329.8 7739.9 -63603.430
15 1173.9 8096.7 -66836.410
16 966.3 10964.0 -101197.010
17 799.2 32662.0 -305534.340
18 164.5 124000.0 -1579972.665

这只是一次迭代。我是否遗漏了一些明显的东西?

我可以创建一个数据帧并在 for 循环中打印它,如下所示:

for i in range(1,31):
result = find_peaks(df1['R'], df1['I {}'.format(i)])
df2 = pd.DataFrame(result)
print df2

输出:

0   3053.3   105000.0  -5264681.265
1 3016.0 82135.0 -3996564.000
2 2962.8 99083.0 -2068656.650
3 2936.2 68118.0 -1144331.400
4 2906.3 2540000.0 -12448747.450
5 2871.3 102000.0 -676631.550
6 2777.9 8518.1 -68760.440
7 2719.3 11823.0 -91604.700
8 2625.0 5944.9 -40842.200
9 2598.3 5341.4 -69304.580
10 2573.5 18013.0 -171000.100
11 1538.9 12768.0 -963665.665
12 1467.7 9313.0 -140466.955
13 1451.4 21736.0 -282040.645
14 1329.9 7766.8 -64290.480
15 1173.8 8124.5 -66890.800
16 966.3 10992.0 -101581.670
17 799.2 32813.0 -306268.920
18 165.1 125000.0 -1518163.855
0 1 2
0 3053.5 107000.0 -5341438.795
1 3015.8 83495.0 -4095259.900
2 2962.6 101000.0 -1192498.950
3 2936.2 69227.0 -1158840.650
4 2906.3 2590000.0 -22633675.700
5 2871.4 104000.0 -678075.650
6 2777.9 8664.1 -71047.000
7 2719.3 11970.0 -94091.010
8 2625.0 6062.4 -43625.710
9 2599.0 5396.6 -85182.360
10 2573.5 18324.0 -175058.890
11 1539.3 12990.0 -976482.225
12 1467.7 9439.5 -144544.535
13 1451.4 22168.0 -287473.935
14 1329.8 7876.6 -65722.160
15 1173.9 8271.3 -69162.670
16 966.2 11175.0 -104500.590
17 799.2 33359.0 -312364.800
18 164.7 126000.0 -1590673.985
0 1 2
0 3053.4 105000.0 -5251114.775
1 3015.6 82076.0 -4009585.150
2 2962.8 98884.0 -2072324.600
3 2936.2 67976.0 -1147359.800
4 2906.3 2540000.0 -22141112.700
5 2871.3 102000.0 -676034.450
6 2835.5 5922.0 -52541.315
7 2777.9 8499.6 -68753.590
8 2719.3 11826.0 -92101.350
9 2624.9 5973.3 -40374.010
10 2599.4 5334.5 -70866.150
11 2573.5 18080.0 -171285.830
12 1536.7 12711.0 -986090.675
13 1467.7 9281.1 -148301.345
14 1451.4 21621.0 -280091.420
15 1329.8 7723.2 -62916.410
16 1173.9 8101.7 -66870.850
17 966.3 11012.0 -101670.860
18 799.2 32737.0 -306106.330
19 164.9 121000.0 -1517466.745
0 1 2
0 3053.6 106000.0 -5266423.625
1 3016.0 82644.0 -4073279.700
2 2962.8 99532.0 -2060277.500
3 2936.2 68554.0 -1146571.550
4 2906.4 2560000.0 -12269729.200
5 2871.5 103000.0 -659901.500
6 2777.9 8576.3 -70258.860
7 2719.3 11861.0 -92537.130
8 2625.0 5996.9 -42155.310
9 2599.2 5337.1 -75180.410
10 2573.5 18094.0 -172402.780
11 1536.4 12806.0 -996597.325
12 1467.8 9338.6 -141607.480
13 1451.5 21850.0 -282960.420
14 1329.9 7786.9 -63955.860
15 1173.8 8170.9 -67855.160
16 966.3 11090.0 -102342.550
17 799.2 33048.0 -308337.090
18 165.0 127000.0 -1541565.995
0 1 2
0 3053.8 106000.0 -5227895.505
1 3016.3 82252.0 -4041714.650
2 2962.8 99252.0 -2068891.400
3 2936.2 68293.0 -1142098.500
4 2906.4 2550000.0 -12225484.750
5 2871.6 103000.0 -647924.800
6 2777.9 8540.5 -69038.240
7 2719.3 11868.0 -92378.180
8 2624.9 5951.6 -40822.570
9 2599.2 5340.6 -71906.480
10 2573.5 18078.0 -171680.150
11 1536.5 12812.0 -992674.925
12 1467.7 9319.3 -142013.155
13 1451.4 21797.0 -282878.100
14 1329.9 7768.6 -63744.750
15 1173.9 8115.4 -66986.810
16 966.2 11044.0 -101923.130
17 799.2 32852.0 -307205.630
18 165.0 127000.0 -1535242.695
0 1 2
0 3053.3 105000.0 -5272282.375
1 3016.0 82104.0 -4056633.200
2 2962.8 98982.0 -2045289.400
3 2936.2 68047.0 -1144401.250
4 2906.3 2540000.0 -12453831.150
5 2871.3 102000.0 -676913.250
6 2777.9 8541.3 -68876.700
7 2719.3 11793.0 -91510.060
8 2624.9 5955.1 -40273.660
9 2599.3 5324.5 -72313.440
10 2573.5 18027.0 -171148.290
11 1536.1 12724.0 -994381.815
12 1467.7 9312.6 -144574.545
13 1451.4 21750.0 -282105.360
14 1329.8 7751.1 -63726.170
15 1173.9 8127.7 -67032.530
16 966.2 11037.0 -102581.530
17 799.2 32751.0 -306267.150
18 164.7 124000.0 -1553345.655
0 1 2
0 3053.6 106000.0 -5265189.215
1 3016.0 82688.0 -4036892.550
2 2962.8 99517.0 -2069015.050
3 2936.2 68500.0 -1151194.550
4 2906.4 2560000.0 -12267708.100
5 2871.5 103000.0 -660027.000
6 2836.0 5955.5 -52472.160
7 2777.9 8564.5 -70173.640
8 2719.3 11870.0 -92484.110
9 2624.9 5982.4 -40998.670
10 2599.1 5356.2 -74299.320
11 2573.5 18068.0 -172105.180
12 1536.6 12815.0 -1533879.740
13 1467.7 9355.2 -143629.490
14 1451.4 21794.0 -283170.690
15 1329.9 7793.6 -64542.480
16 1173.8 8173.6 -67396.560
17 966.3 11094.0 -102249.720
18 799.2 33038.0 -308504.080
19 164.6 126000.0 -1574569.585
0 1 2
0 3053.5 105000.0 -5231625.195
1 3015.7 81935.0 -4022440.400
2 2962.8 98586.0 -2053660.550
3 2936.2 67986.0 -1137495.700
4 2906.2 2530000.0 -22122602.300
5 2871.4 102000.0 -665178.600
6 2777.9 8478.3 -68021.330
7 2719.3 11841.0 -91483.290
8 2625.0 5918.1 -39070.060
9 2599.7 5311.7 -67478.330
10 2573.5 17936.0 -169218.520
11 1536.8 12678.0 -982684.305
12 1467.7 9274.0 -140698.360
13 1451.5 21654.0 -280552.405
14 1329.9 7742.2 -63104.470
15 1173.9 8095.0 -66294.820
16 966.3 10966.0 -101201.140
17 799.2 32731.0 -305697.550
18 165.3 127000.0 -1486712.525
0 1 2
0 3053.6 106000.0 -5274665.865
1 3016.1 82674.0 -4071774.300
2 2962.8 99558.0 -2066219.100
3 2936.2 68496.0 -1146350.850
4 2906.4 2560000.0 -12278707.700
5 2871.4 103000.0 -670961.150
6 2777.9 8560.6 -69495.830
7 2719.3 11880.0 -92625.860
8 2625.0 5993.5 -41590.530
9 2599.2 5346.2 -73230.510
10 2573.5 18136.0 -172528.610
11 1539.4 12772.0 -958919.915
12 1535.9 12768.0 -12767.900
13 1467.7 9347.7 -139666.010
14 1451.4 21809.0 -282404.725
15 1329.8 7789.6 -63997.730
16 1173.9 8130.6 -67136.140
17 966.3 11016.0 -101790.200
18 799.2 32893.0 -307478.280
19 164.7 123000.0 -1558789.125
0 1 2
0 3053.5 107000.0 -5345089.435
1 3015.7 83656.0 -4118951.550
2 2962.5 101000.0 -1202811.850
3 2936.2 69268.0 -1164345.750
4 2906.3 2590000.0 -22621991.900
5 2871.4 104000.0 -677872.950
6 2777.9 8667.3 -71646.060
7 2719.3 12013.0 -94081.880
8 2625.0 6049.8 -43541.960
9 2599.1 5452.6 -83798.440
10 2573.5 18328.0 -174702.070
11 1536.6 12943.0 -1009748.295
12 1467.7 9443.4 -145409.970
13 1451.4 22013.0 -286456.020
14 1329.9 7889.0 -65194.250
15 1173.8 8249.0 -69109.850
16 966.3 11194.0 -104514.330
17 799.2 33336.0 -311816.700
18 165.5 125000.0 -1486136.215
0 1 2
0 3053.6 106000.0 -5275056.805
1 3015.8 82725.0 -4068048.550
2 2962.8 99561.0 -2077418.750
3 2936.2 68533.0 -1147110.850
4 2906.3 2560000.0 -12539436.300
5 2871.4 103000.0 -671279.450
6 2835.9 5963.3 -55389.310
7 2777.9 8598.8 -69739.830
8 2719.3 11854.0 -92473.400
9 2624.9 5977.1 -41511.950
10 2599.6 5361.0 -77447.630
11 2573.5 18219.0 -173562.890
12 1540.0 12806.0 -954739.515
13 1467.7 9375.3 -142509.735
14 1451.4 21896.0 -284104.170
15 1329.9 7819.0 -64769.420
16 1173.9 8131.5 -68330.980
17 966.3 11124.0 -103195.000
18 799.2 33013.0 -308627.230
19 164.7 123000.0 -1559588.815
0 1 2
0 3053.9 106000.0 -5218189.265
1 3015.7 82293.0 -4028909.800
2 2962.8 99166.0 -2068491.600
3 2936.2 68306.0 -1147128.800
4 2906.2 2540000.0 -22238317.650
5 2871.7 103000.0 -636980.450
6 2835.9 5971.2 -57863.995
7 2777.9 8512.7 -69450.540
8 2719.3 11809.0 -92201.830
9 2625.0 5985.2 -40995.630
10 2599.2 5352.3 -73126.000
11 2573.5 18030.0 -171891.810
12 1537.8 12752.0 -975686.025
13 1467.7 9298.2 -138731.715
14 1451.4 21755.0 -282701.130
15 1329.9 7754.0 -64325.840
16 1173.9 8114.2 -66967.060
17 966.3 11050.0 -101948.170
18 799.2 32784.0 -306785.160
19 165.3 126000.0 -1497125.745
0 1 2
0 3053.6 106000.0 -5275229.935
1 3015.7 82698.0 -4074245.600
2 2962.8 99572.0 -2077340.800
3 2936.2 68556.0 -1146676.950
4 2906.3 2560000.0 -22320190.500
5 2871.4 103000.0 -671535.500
6 2777.9 8570.0 -69896.250
7 2719.3 11858.0 -92532.730
8 2624.9 6033.0 -41775.810
9 2598.6 5387.3 -75338.280
10 2573.5 18132.0 -172472.510
11 1539.0 12775.0 -966657.215
12 1535.5 12778.0 -29386.350
13 1467.8 9352.4 -142225.165
14 1451.4 21890.0 -285449.315
15 1329.9 7822.7 -64746.510
16 1173.8 8140.2 -67212.060
17 966.3 11083.0 -102853.620
18 799.2 33027.0 -308602.830
19 164.8 131000.0 -1578998.685
0 1 2
0 3053.6 106000.0 -5263073.355
1 3015.5 82580.0 -4044575.150
2 2962.8 99445.0 -2068541.250
3 2936.2 68477.0 -1145501.100
4 2906.2 2550000.0 -22300336.250
5 2871.5 103000.0 -660097.650
6 2777.9 8540.8 -69562.150
7 2719.3 11851.0 -92301.820
8 2624.9 5980.8 -41526.840
9 2599.5 5335.7 -75110.870
10 2573.5 18068.0 -171535.690
11 1538.5 12785.0 -969030.135
12 1467.7 9325.5 -142824.245
13 1451.4 21799.0 -282827.240
14 1329.9 7812.1 -64583.570
15 1173.9 8115.1 -66995.610
16 966.3 11083.0 -102866.740
17 799.2 32962.0 -307343.050
18 164.9 126000.0 -1542190.545
0 1 2
0 3054.0 107000.0 -5254248.545
1 3015.7 83028.0 -4108625.450
2 2962.6 100000.0 -2067665.150
3 2936.2 68815.0 -1161923.900
4 2906.3 2570000.0 -12601367.400
5 2871.7 104000.0 -643512.350
6 2836.5 5988.1 -53758.345
7 2777.9 8578.2 -70396.170
8 2719.3 11910.0 -93357.430
9 2625.0 6025.3 -42258.880
10 2599.5 5387.1 -76733.960
11 2573.5 18205.0 -173865.510
12 1536.9 12929.0 -997412.535
13 1467.7 9393.4 -142291.345
14 1451.5 21926.0 -285105.860
15 1329.9 7845.1 -64911.120
16 1173.8 8214.6 -67710.700
17 966.3 11111.0 -102503.330
18 799.2 33129.0 -309381.350
19 164.8 127000.0 -1571130.525
0 1 2
0 3053.6 106000.0 -5267792.005
1 3015.9 82644.0 -4060987.250
2 2962.8 99468.0 -2057845.450
3 2936.2 68536.0 -1157006.500
4 2906.3 2560000.0 -22307607.650
5 2871.4 103000.0 -670895.350
6 2777.9 8583.5 -69804.640
7 2719.3 11836.0 -92987.500
8 2624.9 5953.0 -41451.960
9 2599.3 5352.6 -76272.890
10 2573.5 18124.0 -172454.110
11 1540.5 12801.0 -945507.175
12 1535.9 12797.0 -28148.300
13 1467.6 9361.4 -145567.795
14 1451.4 21839.0 -283676.415
15 1329.9 7785.7 -64598.020
16 1173.9 8183.1 -67436.530
17 966.3 11139.0 -102620.090
18 799.2 32986.0 -308119.790
19 164.7 126000.0 -1572891.775
0 1 2
0 3053.5 105000.0 -5237388.615
1 3016.1 82009.0 -4014431.400
2 2962.8 98746.0 -2049939.050
3 2936.2 67849.0 -1137311.600
4 2906.2 2530000.0 -22132482.750
5 2871.4 102000.0 -664702.900
6 2836.0 5915.9 -55276.240
7 2777.9 8458.7 -68503.080
8 2719.3 11802.0 -91989.180
9 2625.0 5934.3 -40186.620
10 2600.1 5329.6 -72514.720
11 2573.5 18007.0 -170792.560
12 1536.2 12701.0 -990763.865
13 1467.7 9302.4 -142308.070
14 1451.5 21725.0 -280484.480
15 1329.9 7724.9 -63005.910
16 1173.9 8085.5 -66164.990
17 966.3 10955.0 -101214.880
18 799.2 32674.0 -305620.780
19 164.8 125000.0 -1561357.455
0 1 2
0 3053.4 106000.0 -5304722.875
1 3015.5 82844.0 -4074342.100
2 2962.8 99899.0 -2076876.400
3 2936.2 68785.0 -1150593.650
4 2906.2 2560000.0 -22409747.550
5 2871.3 103000.0 -681983.000
6 2836.0 5958.7 -53100.070
7 2777.9 8602.1 -70418.760
8 2719.3 11903.0 -93311.480
9 2624.9 6025.5 -41750.370
10 2599.1 5383.1 -76037.880
11 2573.5 18158.0 -172767.880
12 1537.5 12840.0 -987880.945
13 1467.7 9377.7 -143456.640
14 1451.4 21918.0 -284535.710
15 1329.8 7827.3 -64834.370
16 1173.8 8174.6 -67394.820
17 966.3 11102.0 -103079.590
18 799.2 33071.0 -308927.600
19 165.5 127000.0 -1484748.275
0 1 2
0 3054.2 108000.0 -5282263.645
1 3016.0 83751.0 -4144337.300
2 2962.2 101000.0 -1226911.700
3 2936.2 69404.0 -1171643.200
4 2906.2 2590000.0 -22663290.050
5 2871.3 104000.0 -689998.350
6 2777.9 8693.1 -72406.270
7 2719.3 12020.0 -94385.590
8 2625.0 6073.9 -43597.730
9 2599.0 5464.3 -84262.710
10 2573.5 18401.0 -175987.390
11 1536.5 12992.0 -1017452.595
12 1467.7 9454.6 -140987.550
13 1451.4 22124.0 -288333.285
14 1329.9 7903.2 -65918.720
15 1173.9 8285.6 -69334.220
16 966.3 11237.0 -104160.440
17 799.2 33369.0 -312236.890
18 164.8 130000.0 -1572552.825
0 1 2
0 3054.1 106000.0 -5189301.495
1 3015.8 82240.0 -4011853.800
2 2962.8 98961.0 -2064739.450
3 2936.2 68133.0 -1144886.500
4 2906.3 2540000.0 -12457070.250
5 2871.3 102000.0 -676695.700
6 2777.9 8526.4 -69498.320
7 2719.3 11777.0 -91936.940
8 2625.0 5936.6 -39681.480
9 2598.7 5348.4 -73702.570
10 2573.5 18048.0 -172057.340
11 1538.9 12763.0 -961872.385
12 1467.7 9284.1 -143518.630
13 1451.4 21726.0 -282382.630
14 1329.8 7785.4 -63853.560
15 1173.8 8137.5 -67009.300
16 966.3 11032.0 -101826.890
17 799.2 32894.0 -307287.910
18 164.7 128000.0 -1575549.575
0 1 2
0 3054.2 107000.0 -5228854.295
1 3015.9 83051.0 -4073552.000
2 2962.8 99953.0 -2074151.350
3 2936.2 68786.0 -1160861.550
4 2906.4 2570000.0 -12319461.650
5 2871.7 104000.0 -642990.150
6 2777.8 8593.5 -70518.000
7 2719.3 11898.0 -93291.320
8 2625.0 5989.5 -42171.470
9 2599.3 5409.8 -79992.940
10 2573.5 18183.0 -173722.830
11 1537.4 12858.0 -992450.685
12 1467.8 9378.1 -142761.200
13 1451.4 21896.0 -284700.280
14 1329.9 7853.0 -64950.390
15 1173.9 8199.2 -68133.690
16 966.3 11067.0 -102855.720
17 799.2 32989.0 -308465.540
18 165.0 127000.0 -1532980.725
0 1 2
0 3053.7 105000.0 -5205321.345
1 3015.8 81751.0 -4035781.050
2 2962.8 98485.0 -2049725.500
3 2936.2 67726.0 -1139161.450
4 2906.4 2530000.0 -12135887.450
5 2871.5 102000.0 -653477.050
6 2777.9 8481.3 -68483.020
7 2719.3 11728.0 -91079.990
8 2624.9 5908.7 -40136.220
9 2598.6 5284.4 -69483.640
10 2573.5 17909.0 -170137.490
11 1537.6 12681.0 -969209.985
12 1467.7 9229.2 -139920.115
13 1451.4 21627.0 -280030.785
14 1329.9 7729.6 -63447.630
15 1173.8 8063.4 -66632.980
16 966.3 10945.0 -100966.670
17 799.2 32577.0 -304289.400
18 164.7 125000.0 -1559196.095
0 1 2
0 3053.4 105000.0 -5259119.795
1 3016.0 82181.0 -4039385.150
2 2962.8 98913.0 -2057664.750
3 2936.2 68078.0 -1139864.650
4 2906.3 2540000.0 -22205482.350
5 2871.3 102000.0 -676008.150
6 2836.4 5948.2 -55511.295
7 2777.9 8524.4 -69375.060
8 2719.3 11772.0 -91256.040
9 2625.0 5959.7 -40911.190
10 2599.2 5346.9 -71494.100
11 2573.5 18043.0 -171249.490
12 1536.1 12767.0 -1523369.720
13 1467.7 9250.6 -141423.685
14 1451.4 21703.0 -281430.525
15 1329.9 7736.5 -64187.780
16 1173.9 8092.8 -66922.640
17 966.3 10996.0 -101410.790
18 799.2 32704.0 -305713.600
19 165.4 126000.0 -1482283.955
0 1 2
0 3053.6 105000.0 -5217775.735
1 3015.9 81892.0 -4013311.400
2 2962.8 98694.0 -2050799.050
3 2936.2 67884.0 -1140645.600
4 2906.3 2530000.0 -22099575.600
5 2871.5 102000.0 -653778.650
6 2777.9 8482.4 -68580.440
7 2719.3 11768.0 -91285.610
8 2625.0 5902.5 -40623.500
9 2599.3 5304.7 -69163.680
10 2573.5 18009.0 -170745.690
11 1538.0 12694.0 -965128.025
12 1467.7 9279.2 -144139.995
13 1451.4 21626.0 -280386.495
14 1329.8 7739.9 -63603.430
15 1173.9 8096.7 -66836.410
16 966.3 10964.0 -101197.010
17 799.2 32662.0 -305534.340
18 164.5 124000.0 -1579972.665

但是,我无法将数据帧导出到 csv,因为它只导出一个循环。

谢谢

J

最佳答案

您将在此处覆盖每次迭代的结果:

for i in range(1,31):
result = find_peaks(df1['R'], df1['I {}'.format(i)])

我建议创建一个 dfs 列表,然后concat它们:

result=[]
for i in range(1,31):
result.append(pd.DataFrame(find_peaks(df1['R'], df1['I {}'.format(i)])))

df2 = pd.concat(result, ignore_index=True)

关于python - 存储单个函数的迭代结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com