2017-04-10 15 views
1

私は、DataFrameの 'ID'グループに機能を適用してかなりのクリーンアップを必要とするpandas DataFrameにデータを持っています。どのようにPandas DataFrameグループを操作するために任意の機能を適用しますか?データフレームの簡単な例は以下である:関数を適用してPython Pandas DataFrameグループを操作する

import pandas as pd 
import numpy as np 

waypoint_time_string = ['0.5&3.0&6.0' for x in range(10)] 
moving_string = ['0 0 0&0 0.1 0&1 1 1.2' for x in range(10)] 

df = pd.DataFrame({'ID':[1,1,1,1,1,2,2,2,2,2], 'time':[1,2,3,4,5,1,2,3,4,5], 
     'X':[0,0,0,0,0,1,1,1,1,1],'Y':[0,0,0,0,0,1,1,1,1,1],'Z':[0,0,0,0,0,1,1,1,1,1], 
     'waypoint_times':waypoint_time_string, 
     'moving':moving_string}) 

Iがdfの各「ID」グループに機能set_group_positions(以下に定義)を適用したいです。私はDataFrameをループするだけで成功しました。これを行うためのより多くの 'Pandas.groupby'の方法がなければならないようです。

sub_frames = [] 
unique_IDs = df['ID'].unique() 
for unique_ID in unique_IDs: 
    working_df = df.loc[df['ID']==unique_ID] 
    working_df = set_group_positions(working_df) 
    sub_frames.append(working_df) 

final_df = pd.concat(sub_frames) 

と作業の例を完了するために、ここに追加のヘルパー関数:ここで私は交換しているよ私の実装の一例である私の現在の実装では、上で、動作しますが

def set_x_vel(row): 
    return(row['X'] + row['x_movement']) 
def set_y_vel(row): 
    return(row['Y'] + row['y_movement']) 
def set_z_vel(row): 
    return(row['Z'] + row['z_movement']) 

output_time_list = df['time'].unique().tolist() 

#main function to apply to each ID group in the data frame: 
def set_group_positions(df): #pass the combined df here 
    working_df = df 
    times_string = working_df['waypoint_times'].iloc[0] 
    times_list = times_string.split('&') 
    times_list = [float(x) for x in times_list] 
    points_string = working_df['moving'] 
    points_string = points_string.iloc[0] 
    points_list = points_string.split('&') 
    points_x = [] 
    points_y = [] 
    points_z = [] 
    for point in points_list: 
     point_list = point.split(' ') 
     points_x.append(point_list[0]) 
     points_y.append(point_list[1]) 
     points_z.append(point_list[2]) 

    #get corresponding positions for HPAC times, 
    #since there could be mismatches 

    points_x = np.cumsum([float(x) for x in points_x]) 
    points_y = np.cumsum([float(x) for x in points_x]) 
    points_z = np.cumsum([float(x) for x in points_x]) 

    x_interp = np.interp(output_time_list,times_list,points_x).tolist() 
    y_interp = np.interp(output_time_list,times_list,points_y).tolist() 
    z_interp = np.interp(output_time_list,times_list,points_z).tolist() 

    working_df.loc[:,('x_movement')] = x_interp 
    working_df.loc[:,('y_movement')] = y_interp 
    working_df.loc[:,('z_movement')] = z_interp 

    working_df.loc[:,'x_pos'] = working_df.apply(set_x_vel, axis = 1) 
    working_df.loc[:,'y_pos'] = working_df.apply(set_y_vel, axis = 1) 
    working_df.loc[:,'z_pos'] = working_df.apply(set_z_vel, axis = 1) 

    return(working_df) 

は私私のDataFrameでの単純なgroupby.applyラムダコールに数秒から数分かかります。代わりに、ループの

答えて

1

、あなたがgroupbyと関数呼び出しでapplyを使用することができます。

df = df.groupby('ID').apply(set_group_positions) 
+0

Dohのは...これは私が試した最初のものだった宣誓ていたかもしれない:)ありがとうございます。 – Docuemada

+0

問題ありません!お役に立てて嬉しいです。 – ASGM

関連する問題