我编写了一个函数,该函数基于某些组从熊猫数据框中提取数据,然后将整体数据附加到底部。直接调用时有效。当我尝试利用相同的代码创建通用函数时,尝试将两个数据帧附加在一起会崩溃。
工作代码:
#get data by series group
means = self.df.groupby(['motion','test_cycle'],as_index = False)['grip_force'].mean()
mins = self.df.groupby(['motion','test_cycle'],as_index = False)['grip_force'].min()
maxs = self.df.groupby(['motion','test_cycle'],as_index = False)['grip_force'].max()
stds = self.df.groupby(['motion','test_cycle'],as_index = False)['grip_force'].std()
# organize the data
means.columns = ['motion','test_cycle','avg_grip_force']
means['min_grip_force'] = mins['grip_force']
means['max_grip_force'] = maxs['grip_force']
means['stds'] = stds['grip_force']
def add_two_stds(row):
return row['avg_grip_force'] + 2.0 * row['stds']
means['avg_plus_two_stds'] = means.apply(add_two_stds, axis=1)
# add overall averages
overalls = [0,0,self.df['grip_force'].mean(),self.df['grip_force'].min(),self.df['grip_force'].max(),self.df['grip_force'].std()]
overalls.append(overalls[2] + 2.0* overalls[5])
cols = ['motion','test_cycle','avg_grip_force','min_grip_force','max_grip_force','stds', 'avg_plus_two_stds']
overall_frame = pd.DataFrame([overalls],columns=cols)
# THE BELOW LINE FUNCTIONS PROPERLY
total_df = means.append(overall_frame, ignore_index=True)
但是以下代码不起作用:
def get_descriptive_stats(self, data_tag, groups = []):
# input data field is a column name in the data field self.df
# returns data frame with results
# create columns
cols = []
overall = []
for i in groups:
#add group tags to the front of the data frame columns
cols.append(i)
# add a generic zero term to the overall frame as there is no group data
overall.append(0)
# add avg_, min_, max_, std_, avg+std_ tags to column outpus
cols.append('avg_' + data_tag)
cols.append('min_' + data_tag)
cols.append('max_' + data_tag)
cols.append('std_' + data_tag)
cols.append('avg_plus_two_stds_' + data_tag)
#out_frame = pd.DataFrame(columns=cols)
if len(groups) > 0:
#get data by series group
means = self.df.groupby(groups,as_index = False)[data_tag].mean()
mins = self.df.groupby(groups,as_index = False)[data_tag].min()
maxs = self.df.groupby(groups,as_index = False)[data_tag].max()
stds = self.df.groupby(groups,as_index = False)[data_tag].std()
# organize the data
means.columns = [cols[0:(len(cols)-4)]]
means[cols[len(cols)-4]] = mins[data_tag]
means[cols[len(cols)-3]] = maxs[data_tag]
means[cols[len(cols)-2]] = stds[data_tag]
def add_two_stds(row):
return (row[cols[len(cols)-5]].iloc[0] + 2.0 * row[cols[len(cols)-2]].iloc[0])
means[cols[len(cols)-1]] = means.apply(add_two_stds, axis=1)
out_frame_1 = means
# get overall frame data
avg = self.df[data_tag].mean()
std = self.df[data_tag].std()
overall.append(avg)
overall.append(self.df[data_tag].min())
overall.append(self.df[data_tag].max())
overall.append(std)
overall.append(avg+2.0*std)
overall_frame = pd.DataFrame([overall],columns=cols)
if len(groups)>0:
###################
#THIS CODE RETURNS AttributeError: 'NoneType' dobject has no attribute 'is_extension'
###################
out_frame = out_frame_1.append(overall_frame, ignore_index=True)
else:
out_frame = overall_frame
return out_frame
我知道有些奇怪,因为我必须将.iloc []功能添加到apply函数中。但是我已经检查了所有数据类型,它们都是DataFrames。感谢您的帮助
答案 0 :(得分:0)
找到了答案。我无意中将多索引标头添加到了无效代码块中的数据帧out_frame_1中。
我替换了代码行(请注意,稍后意味着将其重新分配给out_frame_1):
means.columns = [cols[0:(len(cols)-4)]]
具有:
means.columns = cols[0:(len(cols)-4)]
生成的单索引列名称可以附加。