2017-09-22 3 views
-1

Ich lernte Resampling zu einem Pandas Datenrahmen anzuwenden, dass resample nicht mit nicht numerischen Wert von Resampling pandas dataframe is deleting column und ich würde, angewendet werden wie resample('30S') an den Eingang anzuwenden df wie im folgenden:Wie mit nicht numerischen Wert

Input_DF:

eventTime   uuid ts     m_op op.prg op.tr  w.cycle cycle_type 
0 2017-04-27 01:22:22 id1 2017-04-27 02:30:30 w  0.0  01:34:48 3  type_a              
1 2017-04-27 01:23:16 id1 2017-04-27 02:31:00 w  1.0  01:33:54 3  type_a              
2 2017-04-27 01:25:10 id1 2017-04-27 02:41:00 w  2.0  01:33:00 3  type_a              
3 2017-04-27 01:25:32 id1 2017-04-27 02:42:45 w  3.0  01:32:00 3  type_a              
4 2017-04-27 01:25:45 id1 2017-04-27 02:52:45 r  4.0  01:32:00 2  type_a              

Output_DF

eventTime   uuid ts     m_op op.prg op.tr  w.cycle cycle_type 
0 2017-04-27 01:22:30 id1 2017-04-27 02:30:30 w  0.0  01:34:48 3  type_a              
1 2017-04-27 01:23:00 id1 2017-04-27 02:30:30 w  0.0  01:34:48 3  type_a              
2 2017-04-27 01:23:30 id1 2017-04-27 02:31:00 w  1.0  01:33:54 3  type_a              
3 2017-04-27 01:24:00 id1 2017-04-27 02:31:00 w  1.0  01:33:54 3  type_a              
4 2017-04-27 01:24:30 id1 2017-04-27 02:31:00 w  1.0  01:33:54 3  type_a              
5 2017-04-27 01:25:00 id1 2017-04-27 02:31:00 w  1.0  01:33:54 3  type_a              
6 2017-04-27 01:25:30 id1 2017-04-27 02:41:00 w  2.0  01:33:00 3  type_a              
7 2017-04-27 01:26:00 id1    avg +popular 3.5  Avg  +popular  type_a     

, wobei avg_of_the_values den Durchschnittswert unter den entsprechenden Zeiten berechnen, +popular füllen Sie mit dem beliebtesten Wert oder der erste - im Falle von zwei Werten mit dem gleichen Rang - und Avg ist der übliche Mittelwert.

Ich habe groupBy Ansatz angewendet, aber es hat nicht funktioniert.
Jeder Vorschlag würde im Voraus sehr geschätzt werden. Vielen Dank im Voraus.carlo

Antwort

0

Eine erste Lösung, die ich implementiert habe, basiert auf der Aufteilung des gesamten Problems in einer Anzahl in Teilprobleme - eines für jede Spalte, die nicht numerisch ist - und dann die erhaltene Lösung zusammenführen. Nachfolgend berichte ich einen Teil des Codes, den ich verwendet habe, um den Fall m_op zu lösen:

smart_sub_schema_Operation_progress=["eventTime","uuid", "m_op"]  
operation_progress_sub_smart_home_df=smart_home_df[smart_sub_schema_Operation_progress] 
operation_progress_sub_smart_home_df['m_op'] = operation_progress_sub_smart_home_df['m_op'].map({'P':0, 'W': 1, 'R': 2, 'S': 3, 'F': 4}) 
operation_progress_sub_smart_home_df.eventTime = pd.to_datetime(operation_progress_sub_smart_home_df.eventTime) 
operation_progress_sub_smart_home_df.index = operation_progress_sub_smart_home_df['eventTime'] 
resampled_operation_progress_sub_smart_home_df=operation_progress_sub_smart_home_df.resample('30S').reset_index() 
resampled_operation_progress_sub_smart_home_df["m_op"]=resampled_operation_progress_sub_smart_home_df["m_op"].astype(float) 
resampled_operation_progress_sub_smart_home_df.fillna(method='ffill', inplace=True) 
resampled_operation_progress_sub_smart_home_df['m_op'] = resampled_operation_progress_sub_smart_home_df['m_op'].map({0.0:'P', 1.0:'W', 2.0:'R', 3.0:'S', 4.0:'F'}) 
print(resampled_operation_progress_sub_smart_home_df.to_string()) 
Verwandte Themen