2017-04-10 75 views
1

我有一個pandas DataFrame中的數據,需要使用適用於DataFrame'ID'組的功能進行大量清理。如何應用任意函數來操作Pandas DataFrame組?數據幀的簡化例子如下:應用函數來操縱Python熊貓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}) 

我想功能set_group_positions(如下定義)應用到每個「ID」組的df。我只通過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) 

雖然我目前工作的實施,在我真正的數據集,我需要運行大約20分鐘,在我的DataFrame上進行簡單的groupby.apply lambda調用只需幾秒到一分鐘。

回答

1

而是循環的,你可以使用applygroupby和函數調用:

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

衛生署...敢發誓,這是我想的第一件事:)謝謝。 – Docuemada

+0

沒問題!很高興我能幫上忙。 – ASGM