我需要用一个矩阵(第二个数据帧)中的值更新一个数据帧中的一列。必须使用来自第一个数据帧的两个列值来收集矩阵中的值。 第一个数据帧具有30000行以上的数据,矩阵数据帧为613行* 72列数据帧。
这是我正在使用的代码的一部分:
reference_data = reference_data[['Timestamp', 'Windspeed_avg', 'Power_avg', 'NacellePosition']]
cols = np.arange(0, 365, self.wdbin)
rows = np.arange(0, self.maxpower, self.pbin)
reference_data['col_id'] = pd.cut(reference_data['NacellePosition'], cols, include_lowest=True, right=False)
reference_data['row_id'] = pd.cut(reference_data['Power_avg'], rows, include_lowest=True, right=False)
df = reference_data.groupby(['row_id', 'col_id']).size().reset_index(name='Timestamp')
reference_data[colname] = 0
pref = pd.DataFrame()
for index, matrixbin in df.iterrows():
rowmask = (reference_data['row_id'] == matrixbin['row_id'])
colmask = (reference_data['col_id'] == matrixbin['col_id'])
data = reference_data.loc[(rowmask & colmask), 'Power_avg'] * dfmatrix.at['%s' % matrixbin['row_id'], '%s' % matrixbin['col_id']])
pref = pref.append(data)
reference_data[colname] = pref
这需要花费几分钟的时间来计算,并且我要计算几个文件。
我有比遍历所有记录更有效的方法吗?
index,"[0, 5)","[5, 10)","[10, 15)","[15, 20)","[20, 25)","[25, 30)","[30, 35)","[35, 40)","[40, 45)","[45, 50)","[50, 55)","[55, 60)","[60, 65)","[65, 70)","[70, 75)","[75, 80)","[80, 85)","[85, 90)","[90, 95)","[95, 100)","[100, 105)","[105, 110)","[110, 115)","[115, 120)","[120, 125)","[125, 130)","[130, 135)","[135, 140)","[140, 145)","[145, 150)","[150, 155)","[155, 160)","[160, 165)","[165, 170)","[170, 175)","[175, 180)","[180, 185)","[185, 190)","[190, 195)","[195, 200)","[200, 205)","[205, 210)","[210, 215)","[215, 220)","[220, 225)","[225, 230)","[230, 235)","[235, 240)","[240, 245)","[245, 250)","[250, 255)","[255, 260)","[260, 265)","[265, 270)","[270, 275)","[275, 280)","[280, 285)","[285, 290)","[290, 295)","[295, 300)","[300, 305)","[305, 310)","[310, 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.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.9624484004127967,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.321594427244582,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
"[20, 25)",1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,0.9746376811594202,1.2383549783549783,1.0,1.1672077922077921,1.154298418972332,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.7526785714285713,1.0,1.5282738095238095,1.8668478260869565,1.0,1.0,1.0,1.0,1.0,1.829604743083004,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,3.7098814229249006
"[25, 30)",1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.1307758620689654,1.3246021220159152,0.8191862518069415,1.0,0.8567414705345738,1.3924671008004343,1.0483611216369837,1.0,1.0,1.2705979537703675,1.3112290008841734,1.5214489214489215,0.6861263736263736,1.0,1.0,1.0,1.4574106634106634,1.0,1.5274725274725276
Timestamp,Windspeed_avg,Power_avg,NacellePosition,col_id,row_id,Pref1150054
2018-01-01 04:30:00,10.2,2755,229,"[225, 230)","[2755, 2760)",0
2018-01-01 04:40:00,9.6,2541,226,"[225, 230)","[2540, 2545)",0
2018-01-01 04:50:00,10.4,2936,228,"[225, 230)","[2935, 2940)",0
2018-01-01 05:00:00,10.2,2951,228,"[225, 230)","[2950, 2955)",0
2018-01-01 05:10:00,10.2,2906,227,"[225, 230)","[2905, 2910)",0
2018-01-01 05:20:00,9.3,2312,233,"[230, 235)","[2310, 2315)",0
2018-01-01 05:30:00,9.1,2142,232,"[230, 235)","[2140, 2145)",0
2018-01-01 05:40:00,10.9,3025,236,"[235, 240)","[3025, 3030)",0
2018-01-01 05:50:00,10.8,2991,230,"[230, 235)","[2990, 2995)",0
2018-01-01 06:00:00,11.2,3030,228,"[225, 230)","[3030, 3035)",0
2018-01-01 06:10:00,10.8,3030,224,"[220, 225)","[3030, 3035)",0
2018-01-01 06:20:00,11.4,3030,221,"[220, 225)","[3030, 3035)",0
2018-01-01 06:30:00,12.3,3030,227,"[225, 230)","[3030, 3035)",0
2018-01-01 06:40:00,12.4,3029,227,"[225, 230)","[3025, 3030)",0
2018-01-01 06:50:00,12.6,3029,227,"[225, 230)","[3025, 3030)",0
2018-01-01 07:00:00,13.2,3027,232,"[230, 235)","[3025, 3030)",0
2018-01-01 07:10:00,13.4,3030,232,"[230, 235)","[3030, 3035)",0