Python 使用dataframe中的值进行计算

Python 使用dataframe中的值进行计算,python,pandas,dataframe,linear-regression,Python,Pandas,Dataframe,Linear Regression,我正在从数据帧中选择专用数据,并希望根据我定义的公式进行线性插值: y = y0 + (y1 - y0) * [(x-x0)/(x1-x0)] 例如,我想在秩2.0和3.0之间进行插值,其中所需秩为2.5。计算如下所示: y=-9.080002+(-9.039993-(-9.080002))*[(2.5-2)/(3-2)]=-9.0599975000 其中,代码中的值定义如下: import pandas as pd #define path where the Excel File is

我正在从数据帧中选择专用数据,并希望根据我定义的公式进行线性插值:

y = y0 + (y1 - y0) * [(x-x0)/(x1-x0)]
例如,我想在秩2.0和3.0之间进行插值,其中所需秩为2.5。计算如下所示:

y=-9.080002+(-9.039993-(-9.080002))*[(2.5-2)/(3-2)]=-9.0599975000

其中,代码中的值定义如下:

import pandas as pd

#define path where the Excel File is
path = 'Desktop'
#define file name
filename = 'Test_Calculation.xlsx'
#add to full path
final_path = path + '/' + filename
print(final_path)
#define excel worksheet name
ws_name = 'Test_Sheet'
df = pd.read_excel(final_path, sheet_name=ws_name)

#sort column DataPoint and inclkude column rank
df.sort_values('DataPoint', inplace = True)
df['Rank'] = df['DataPoint'].rank() 

#define the VaR function
def Calc(data):

    Rank = 2.5
    Rank_Up = 3.0
    Rank_Down = 2.0

    Calculation = 0.0

    check_int = isinstance(Rank, int)
    if not check_int:
        for ind in data.index:
            if (data['Rank'][ind] in (Rank_Down, Rank_Up)):
                Calculation = data['DataPoint'][ind] + (data['DataPoint'][ind+1]- data['DataPoint'][ind]) *((Rank-Rank_Down)/(Rank_Up-Rank_Down))
                print(data['DataPoint'][ind], data['Rank'][ind])
                print(data['DataPoint'][ind+1], data['Rank'][ind+1])
                break

    return Calculation


Calc_int = Calc(df)

print(Calc_int)
-9.080002000000007 2.0
-9.360001000000011 1.0
-9.22000150000001
Number  DataPoint
1   -5.910004
2   -9.080002
3   -9.360001
4   -0.779999
5   0.389999
6   0.089996
7   0.009995
8   -0.380005
9   1.139999
10  2.389999
11  2.279999
12  0.089996
13  -1.220001
14  -0.960007
15  -5.419998
16  -6.410004
17  -5.029999
18  -7.529999
19  -7.479996
20  -4.580002
21  -5.589996
22  -6.339996
23  -5.570007
24  -3.520004
25  -5.490005
26  -1.860001
27  -1.589996
28  -2.470001
29  -4.350006
30  -2.630005
31  -3.800003
32  -4.949997
33  -2.770004
34  -6.300003
35  -3.220001
36  -4.949997
37  -7.320007
38  -2.110001
39  -2.020004
40  -1.460007
41  -3.300003
42  -9.039993
43  -6.529999
44  -1.149994
45  2.660004
46  3.940002
47  6.009995
48  6.309998
49  5.75
50  5.070007
51  6.160004
52  5.509995
53  6.75
54  5.119995
55  6.320007
56  6.599992
57  6.302474
58  7.403336
59  10.65633
60  9.634812
61  9.763731
62  8.7323
63  7.760373
64  8.434767
65  10.398461
66  9.029833
67  11.72744
68  21.793869
69  21.089708
70  19.592145
71  17.142479
72  17.221823
73  17.985487
74  15.575486
75  12.501017
76  11.816696
77  13.641543
78  13.373762
79  13.730799
80  13.135748
81  11.320812
82  11.628258
83  14.097763
84  14.712653
85  15.595325
86  17.231735
87  16.349063
88  15.615165
89  16.864786
90  15.793676
91  15.466391
92  14.891164
93  15.615165
94  17.390421
95  17.529268
96  14.315938
97  13.978741
98  16.021778
99  16.99372
100 18.521035
101 18.253254
102 18.778889
103 18.253254
104 20.296306
105 20.583912
106 18.729313
107 20.802102
108 21.912876
109 22.329416
110 22.051723
111 21.635182
112 21.833533
113 22.617037
114 20.861605
115 19.145853
116 19.711152
117 21.258306
118 19.582233
119 18.649969
120 20.365723
121 21.241765
122 20.946467
123 18.603786
124 21.665015
125 21.192546
126 18.05257
127 17.117471
128 17.570264
129 12.766789
130 11.684033
131 13.554246
132 8.150336
133 10.571764
134 11.556082
135 9.754779
136 9.833526
137 10.305994
138 9.794145
139 9.311839
140 9.52839
141 9.498846
142 7.933785
143 6.683697
144 7.983004
145 10.118971
146 7.884566
147 8.071589
148 6.703387
149 10.443798
150 12.048241
151 10.325685
152 7.303822
153 7.303822
154 4.498525
155 4.616639
156 1.860546
157 5.502519
158 4.341031
159 5.335186
160 8.248774
161 6.014368
162 8.652332
163 6.919939
164 8.504691
165 7.963313
166 9.400424
167 8.347196
168 10.256775
169 12.245101
170 10.355213
171 11.083599
172 11.31984
173 9.646503
174 8.435796
175 7.884566
176 6.034059
177 5.71907
178 6.073425
179 3.307494
180 4.262284
181 4.350876
182 3.422291
183 4.898252
184 1.174128
185 -0.14545
186 1.975638
187 -1.865776
188 0.206436
189 2.835809
190 2.356848
191 1.88767
192 -1.337954
193 3.412507
194 3.598227
195 1.08616
196 2.288433
197 3.275663
198 2.914007
199 5.523827
200 4.849376
201 1.555337
202 2.552352
203 5.914805
204 8.407334
205 6.823852
206 6.442642
207 6.892266
208 8.681022
209 8.172753
210 8.583285
211 7.801313
212 4.937344
213 4.12605
214 -1.494351
215 -2.00262
216 -0.389816
217 1.877901
218 7.439657
219 6.892266
220 9.31638
221 8.211845
222 9.590068
223 10.352487
224 10.811894
225 9.003586
226 7.89905
227 8.514855
228 8.299812
229 8.114092
230 8.495302
231 7.293029
232 7.019341
233 7.791529
234 8.241167
235 10.460008
236 10.460008
237 9.120876
238 8.27049
239 9.619391
240 9.805111
241 8.417118
242 7.820866
243 7.097551
244 7.903316
245 9.563419
246 9.602245
247 9.553716
248 8.68969
249 8.825604
250 7.14608
代码如下所示:

import pandas as pd

#define path where the Excel File is
path = 'Desktop'
#define file name
filename = 'Test_Calculation.xlsx'
#add to full path
final_path = path + '/' + filename
print(final_path)
#define excel worksheet name
ws_name = 'Test_Sheet'
df = pd.read_excel(final_path, sheet_name=ws_name)

#sort column DataPoint and inclkude column rank
df.sort_values('DataPoint', inplace = True)
df['Rank'] = df['DataPoint'].rank() 

#define the VaR function
def Calc(data):

    Rank = 2.5
    Rank_Up = 3.0
    Rank_Down = 2.0

    Calculation = 0.0

    check_int = isinstance(Rank, int)
    if not check_int:
        for ind in data.index:
            if (data['Rank'][ind] in (Rank_Down, Rank_Up)):
                Calculation = data['DataPoint'][ind] + (data['DataPoint'][ind+1]- data['DataPoint'][ind]) *((Rank-Rank_Down)/(Rank_Up-Rank_Down))
                print(data['DataPoint'][ind], data['Rank'][ind])
                print(data['DataPoint'][ind+1], data['Rank'][ind+1])
                break

    return Calculation


Calc_int = Calc(df)

print(Calc_int)
-9.080002000000007 2.0
-9.360001000000011 1.0
-9.22000150000001
Number  DataPoint
1   -5.910004
2   -9.080002
3   -9.360001
4   -0.779999
5   0.389999
6   0.089996
7   0.009995
8   -0.380005
9   1.139999
10  2.389999
11  2.279999
12  0.089996
13  -1.220001
14  -0.960007
15  -5.419998
16  -6.410004
17  -5.029999
18  -7.529999
19  -7.479996
20  -4.580002
21  -5.589996
22  -6.339996
23  -5.570007
24  -3.520004
25  -5.490005
26  -1.860001
27  -1.589996
28  -2.470001
29  -4.350006
30  -2.630005
31  -3.800003
32  -4.949997
33  -2.770004
34  -6.300003
35  -3.220001
36  -4.949997
37  -7.320007
38  -2.110001
39  -2.020004
40  -1.460007
41  -3.300003
42  -9.039993
43  -6.529999
44  -1.149994
45  2.660004
46  3.940002
47  6.009995
48  6.309998
49  5.75
50  5.070007
51  6.160004
52  5.509995
53  6.75
54  5.119995
55  6.320007
56  6.599992
57  6.302474
58  7.403336
59  10.65633
60  9.634812
61  9.763731
62  8.7323
63  7.760373
64  8.434767
65  10.398461
66  9.029833
67  11.72744
68  21.793869
69  21.089708
70  19.592145
71  17.142479
72  17.221823
73  17.985487
74  15.575486
75  12.501017
76  11.816696
77  13.641543
78  13.373762
79  13.730799
80  13.135748
81  11.320812
82  11.628258
83  14.097763
84  14.712653
85  15.595325
86  17.231735
87  16.349063
88  15.615165
89  16.864786
90  15.793676
91  15.466391
92  14.891164
93  15.615165
94  17.390421
95  17.529268
96  14.315938
97  13.978741
98  16.021778
99  16.99372
100 18.521035
101 18.253254
102 18.778889
103 18.253254
104 20.296306
105 20.583912
106 18.729313
107 20.802102
108 21.912876
109 22.329416
110 22.051723
111 21.635182
112 21.833533
113 22.617037
114 20.861605
115 19.145853
116 19.711152
117 21.258306
118 19.582233
119 18.649969
120 20.365723
121 21.241765
122 20.946467
123 18.603786
124 21.665015
125 21.192546
126 18.05257
127 17.117471
128 17.570264
129 12.766789
130 11.684033
131 13.554246
132 8.150336
133 10.571764
134 11.556082
135 9.754779
136 9.833526
137 10.305994
138 9.794145
139 9.311839
140 9.52839
141 9.498846
142 7.933785
143 6.683697
144 7.983004
145 10.118971
146 7.884566
147 8.071589
148 6.703387
149 10.443798
150 12.048241
151 10.325685
152 7.303822
153 7.303822
154 4.498525
155 4.616639
156 1.860546
157 5.502519
158 4.341031
159 5.335186
160 8.248774
161 6.014368
162 8.652332
163 6.919939
164 8.504691
165 7.963313
166 9.400424
167 8.347196
168 10.256775
169 12.245101
170 10.355213
171 11.083599
172 11.31984
173 9.646503
174 8.435796
175 7.884566
176 6.034059
177 5.71907
178 6.073425
179 3.307494
180 4.262284
181 4.350876
182 3.422291
183 4.898252
184 1.174128
185 -0.14545
186 1.975638
187 -1.865776
188 0.206436
189 2.835809
190 2.356848
191 1.88767
192 -1.337954
193 3.412507
194 3.598227
195 1.08616
196 2.288433
197 3.275663
198 2.914007
199 5.523827
200 4.849376
201 1.555337
202 2.552352
203 5.914805
204 8.407334
205 6.823852
206 6.442642
207 6.892266
208 8.681022
209 8.172753
210 8.583285
211 7.801313
212 4.937344
213 4.12605
214 -1.494351
215 -2.00262
216 -0.389816
217 1.877901
218 7.439657
219 6.892266
220 9.31638
221 8.211845
222 9.590068
223 10.352487
224 10.811894
225 9.003586
226 7.89905
227 8.514855
228 8.299812
229 8.114092
230 8.495302
231 7.293029
232 7.019341
233 7.791529
234 8.241167
235 10.460008
236 10.460008
237 9.120876
238 8.27049
239 9.619391
240 9.805111
241 8.417118
242 7.820866
243 7.097551
244 7.903316
245 9.563419
246 9.602245
247 9.553716
248 8.68969
249 8.825604
250 7.14608
结果如下所示:

import pandas as pd

#define path where the Excel File is
path = 'Desktop'
#define file name
filename = 'Test_Calculation.xlsx'
#add to full path
final_path = path + '/' + filename
print(final_path)
#define excel worksheet name
ws_name = 'Test_Sheet'
df = pd.read_excel(final_path, sheet_name=ws_name)

#sort column DataPoint and inclkude column rank
df.sort_values('DataPoint', inplace = True)
df['Rank'] = df['DataPoint'].rank() 

#define the VaR function
def Calc(data):

    Rank = 2.5
    Rank_Up = 3.0
    Rank_Down = 2.0

    Calculation = 0.0

    check_int = isinstance(Rank, int)
    if not check_int:
        for ind in data.index:
            if (data['Rank'][ind] in (Rank_Down, Rank_Up)):
                Calculation = data['DataPoint'][ind] + (data['DataPoint'][ind+1]- data['DataPoint'][ind]) *((Rank-Rank_Down)/(Rank_Up-Rank_Down))
                print(data['DataPoint'][ind], data['Rank'][ind])
                print(data['DataPoint'][ind+1], data['Rank'][ind+1])
                break

    return Calculation


Calc_int = Calc(df)

print(Calc_int)
-9.080002000000007 2.0
-9.360001000000011 1.0
-9.22000150000001
Number  DataPoint
1   -5.910004
2   -9.080002
3   -9.360001
4   -0.779999
5   0.389999
6   0.089996
7   0.009995
8   -0.380005
9   1.139999
10  2.389999
11  2.279999
12  0.089996
13  -1.220001
14  -0.960007
15  -5.419998
16  -6.410004
17  -5.029999
18  -7.529999
19  -7.479996
20  -4.580002
21  -5.589996
22  -6.339996
23  -5.570007
24  -3.520004
25  -5.490005
26  -1.860001
27  -1.589996
28  -2.470001
29  -4.350006
30  -2.630005
31  -3.800003
32  -4.949997
33  -2.770004
34  -6.300003
35  -3.220001
36  -4.949997
37  -7.320007
38  -2.110001
39  -2.020004
40  -1.460007
41  -3.300003
42  -9.039993
43  -6.529999
44  -1.149994
45  2.660004
46  3.940002
47  6.009995
48  6.309998
49  5.75
50  5.070007
51  6.160004
52  5.509995
53  6.75
54  5.119995
55  6.320007
56  6.599992
57  6.302474
58  7.403336
59  10.65633
60  9.634812
61  9.763731
62  8.7323
63  7.760373
64  8.434767
65  10.398461
66  9.029833
67  11.72744
68  21.793869
69  21.089708
70  19.592145
71  17.142479
72  17.221823
73  17.985487
74  15.575486
75  12.501017
76  11.816696
77  13.641543
78  13.373762
79  13.730799
80  13.135748
81  11.320812
82  11.628258
83  14.097763
84  14.712653
85  15.595325
86  17.231735
87  16.349063
88  15.615165
89  16.864786
90  15.793676
91  15.466391
92  14.891164
93  15.615165
94  17.390421
95  17.529268
96  14.315938
97  13.978741
98  16.021778
99  16.99372
100 18.521035
101 18.253254
102 18.778889
103 18.253254
104 20.296306
105 20.583912
106 18.729313
107 20.802102
108 21.912876
109 22.329416
110 22.051723
111 21.635182
112 21.833533
113 22.617037
114 20.861605
115 19.145853
116 19.711152
117 21.258306
118 19.582233
119 18.649969
120 20.365723
121 21.241765
122 20.946467
123 18.603786
124 21.665015
125 21.192546
126 18.05257
127 17.117471
128 17.570264
129 12.766789
130 11.684033
131 13.554246
132 8.150336
133 10.571764
134 11.556082
135 9.754779
136 9.833526
137 10.305994
138 9.794145
139 9.311839
140 9.52839
141 9.498846
142 7.933785
143 6.683697
144 7.983004
145 10.118971
146 7.884566
147 8.071589
148 6.703387
149 10.443798
150 12.048241
151 10.325685
152 7.303822
153 7.303822
154 4.498525
155 4.616639
156 1.860546
157 5.502519
158 4.341031
159 5.335186
160 8.248774
161 6.014368
162 8.652332
163 6.919939
164 8.504691
165 7.963313
166 9.400424
167 8.347196
168 10.256775
169 12.245101
170 10.355213
171 11.083599
172 11.31984
173 9.646503
174 8.435796
175 7.884566
176 6.034059
177 5.71907
178 6.073425
179 3.307494
180 4.262284
181 4.350876
182 3.422291
183 4.898252
184 1.174128
185 -0.14545
186 1.975638
187 -1.865776
188 0.206436
189 2.835809
190 2.356848
191 1.88767
192 -1.337954
193 3.412507
194 3.598227
195 1.08616
196 2.288433
197 3.275663
198 2.914007
199 5.523827
200 4.849376
201 1.555337
202 2.552352
203 5.914805
204 8.407334
205 6.823852
206 6.442642
207 6.892266
208 8.681022
209 8.172753
210 8.583285
211 7.801313
212 4.937344
213 4.12605
214 -1.494351
215 -2.00262
216 -0.389816
217 1.877901
218 7.439657
219 6.892266
220 9.31638
221 8.211845
222 9.590068
223 10.352487
224 10.811894
225 9.003586
226 7.89905
227 8.514855
228 8.299812
229 8.114092
230 8.495302
231 7.293029
232 7.019341
233 7.791529
234 8.241167
235 10.460008
236 10.460008
237 9.120876
238 8.27049
239 9.619391
240 9.805111
241 8.417118
242 7.820866
243 7.097551
244 7.903316
245 9.563419
246 9.602245
247 9.553716
248 8.68969
249 8.825604
250 7.14608
Excel文件如下所示:

import pandas as pd

#define path where the Excel File is
path = 'Desktop'
#define file name
filename = 'Test_Calculation.xlsx'
#add to full path
final_path = path + '/' + filename
print(final_path)
#define excel worksheet name
ws_name = 'Test_Sheet'
df = pd.read_excel(final_path, sheet_name=ws_name)

#sort column DataPoint and inclkude column rank
df.sort_values('DataPoint', inplace = True)
df['Rank'] = df['DataPoint'].rank() 

#define the VaR function
def Calc(data):

    Rank = 2.5
    Rank_Up = 3.0
    Rank_Down = 2.0

    Calculation = 0.0

    check_int = isinstance(Rank, int)
    if not check_int:
        for ind in data.index:
            if (data['Rank'][ind] in (Rank_Down, Rank_Up)):
                Calculation = data['DataPoint'][ind] + (data['DataPoint'][ind+1]- data['DataPoint'][ind]) *((Rank-Rank_Down)/(Rank_Up-Rank_Down))
                print(data['DataPoint'][ind], data['Rank'][ind])
                print(data['DataPoint'][ind+1], data['Rank'][ind+1])
                break

    return Calculation


Calc_int = Calc(df)

print(Calc_int)
-9.080002000000007 2.0
-9.360001000000011 1.0
-9.22000150000001
Number  DataPoint
1   -5.910004
2   -9.080002
3   -9.360001
4   -0.779999
5   0.389999
6   0.089996
7   0.009995
8   -0.380005
9   1.139999
10  2.389999
11  2.279999
12  0.089996
13  -1.220001
14  -0.960007
15  -5.419998
16  -6.410004
17  -5.029999
18  -7.529999
19  -7.479996
20  -4.580002
21  -5.589996
22  -6.339996
23  -5.570007
24  -3.520004
25  -5.490005
26  -1.860001
27  -1.589996
28  -2.470001
29  -4.350006
30  -2.630005
31  -3.800003
32  -4.949997
33  -2.770004
34  -6.300003
35  -3.220001
36  -4.949997
37  -7.320007
38  -2.110001
39  -2.020004
40  -1.460007
41  -3.300003
42  -9.039993
43  -6.529999
44  -1.149994
45  2.660004
46  3.940002
47  6.009995
48  6.309998
49  5.75
50  5.070007
51  6.160004
52  5.509995
53  6.75
54  5.119995
55  6.320007
56  6.599992
57  6.302474
58  7.403336
59  10.65633
60  9.634812
61  9.763731
62  8.7323
63  7.760373
64  8.434767
65  10.398461
66  9.029833
67  11.72744
68  21.793869
69  21.089708
70  19.592145
71  17.142479
72  17.221823
73  17.985487
74  15.575486
75  12.501017
76  11.816696
77  13.641543
78  13.373762
79  13.730799
80  13.135748
81  11.320812
82  11.628258
83  14.097763
84  14.712653
85  15.595325
86  17.231735
87  16.349063
88  15.615165
89  16.864786
90  15.793676
91  15.466391
92  14.891164
93  15.615165
94  17.390421
95  17.529268
96  14.315938
97  13.978741
98  16.021778
99  16.99372
100 18.521035
101 18.253254
102 18.778889
103 18.253254
104 20.296306
105 20.583912
106 18.729313
107 20.802102
108 21.912876
109 22.329416
110 22.051723
111 21.635182
112 21.833533
113 22.617037
114 20.861605
115 19.145853
116 19.711152
117 21.258306
118 19.582233
119 18.649969
120 20.365723
121 21.241765
122 20.946467
123 18.603786
124 21.665015
125 21.192546
126 18.05257
127 17.117471
128 17.570264
129 12.766789
130 11.684033
131 13.554246
132 8.150336
133 10.571764
134 11.556082
135 9.754779
136 9.833526
137 10.305994
138 9.794145
139 9.311839
140 9.52839
141 9.498846
142 7.933785
143 6.683697
144 7.983004
145 10.118971
146 7.884566
147 8.071589
148 6.703387
149 10.443798
150 12.048241
151 10.325685
152 7.303822
153 7.303822
154 4.498525
155 4.616639
156 1.860546
157 5.502519
158 4.341031
159 5.335186
160 8.248774
161 6.014368
162 8.652332
163 6.919939
164 8.504691
165 7.963313
166 9.400424
167 8.347196
168 10.256775
169 12.245101
170 10.355213
171 11.083599
172 11.31984
173 9.646503
174 8.435796
175 7.884566
176 6.034059
177 5.71907
178 6.073425
179 3.307494
180 4.262284
181 4.350876
182 3.422291
183 4.898252
184 1.174128
185 -0.14545
186 1.975638
187 -1.865776
188 0.206436
189 2.835809
190 2.356848
191 1.88767
192 -1.337954
193 3.412507
194 3.598227
195 1.08616
196 2.288433
197 3.275663
198 2.914007
199 5.523827
200 4.849376
201 1.555337
202 2.552352
203 5.914805
204 8.407334
205 6.823852
206 6.442642
207 6.892266
208 8.681022
209 8.172753
210 8.583285
211 7.801313
212 4.937344
213 4.12605
214 -1.494351
215 -2.00262
216 -0.389816
217 1.877901
218 7.439657
219 6.892266
220 9.31638
221 8.211845
222 9.590068
223 10.352487
224 10.811894
225 9.003586
226 7.89905
227 8.514855
228 8.299812
229 8.114092
230 8.495302
231 7.293029
232 7.019341
233 7.791529
234 8.241167
235 10.460008
236 10.460008
237 9.120876
238 8.27049
239 9.619391
240 9.805111
241 8.417118
242 7.820866
243 7.097551
244 7.903316
245 9.563419
246 9.602245
247 9.553716
248 8.68969
249 8.825604
250 7.14608

索引器错误是因为
[1::

def Calc(data):

    Rank = 2.5
    Rank_Up = 3.0
    Rank_Down = 2.0

    Calculation = 0.0

    check_int = isinstance(Rank, int)
    if not check_int:
        for ind in data.Rank:
            if (ind in (Rank_Down, Rank_Up)):
                index0 = data.Rank.index[ind-1]
                index1 = data.Rank.index[ind]
                Calculation = data['DataPoint'][index0] + (data['DataPoint'][index1]- data['DataPoint'][index0]) *((Rank-Rank_Down)/(Rank_Up-Rank_Down))
                print(data['DataPoint'][index0], ind)
                print(data['DataPoint'][index1], ind+1)
                break

    return Calculation

嗨,鲍里斯,谢谢你的回答。不知何故,我收到了一个不同的结果,因为我手工计算:-9.0599975000。我收到的是-9.2200015,它不在等级2和等级3之间,而是等级1和等级2之间的数字。你知道如何修复它吗?你能分享你的代码获取-9.2200015吗?我的上述代码结果-9.0599975