【问题标题】:How do I append pandas data sets in a relative function call如何在相对函数调用中附加熊猫数据集
【发布时间】:2018-10-24 00:04:18
【问题描述】:

我编写了一个函数,它根据某些组从 pandas 数据框中提取数据,然后将整体数据附加到底部。它在直接调用时起作用。当我尝试利用相同的代码创建一个通用函数时,尝试将两个数据帧附加在一起时会崩溃。

工作代码:

#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 函数中。但是我检查了所有类型的数据,它们都是 DataFrame。任何帮助表示赞赏

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    找到答案。我无意中在非功能代码块中的数据帧 out_frame_1 中添加了多索引标头。

    我替换了这行代码(注意意思是稍后重新分配给 out_frame_1):

    means.columns = [cols[0:(len(cols)-4)]]
    

    与:

    means.columns = cols[0:(len(cols)-4)]
    

    可以附加生成的单索引列名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      相关资源
      最近更新 更多