函数返回空数据帧

时间:2017-11-13 21:41:52

标签: python pandas dataframe return append

我想创建一个循环来创建一个数据帧,该数据帧收集输入数据帧的行,这些行具有共同点。 我的问题:当我应用该函数时,输出数据帧为空... 但是在循环中有一个打印(输出),我们看到该程序有效..我不明白,我试图改变返回位置,但这不起作用

提前感谢您的帮助!

def group (dataframe, identifiant, output):
    for i in range(len(identifiant)):
        ident = identifiant.loc[i,"IDCTV"]
        # print(ident)


        for j in range(len(dataframe)):
            if dataframe.loc[j,"IDCONTREVENANT"] == ident:
                di = dataframe.loc[j, "DATE_INFRACTION"]
                nt = dataframe.loc[j,"NOTRAIN"]
                genre = dataframe.loc[j,"CODEETATCIVIL"]
                age = dataframe.loc[j,"AGE"]
                # print(di, nt, genre, age)

            for k in range(len(dataframe)):
                if k != j :
                    if dataframe.loc[k,"DATE_INFRACTION"] == di and dataframe.loc[k,"NOTRAIN"] == nt:
                        idgroup = dataframe.loc[k,"IDCONTREVENANT"]
                        genreidgroup = dataframe.loc[k,"CODEETATCIVIL"]
                        ageidgroup = dataframe.loc[k,"AGE"]
                        output = output.append({ "IDREF" : ident ,"CODEETATCIVILREF" : genre,"AGEREF" : age ,"IDCTV" : idgroup,"CODEETATCIVILCTV" : genreidgroup,"AGECTV" : ageidgroup}, ignore_index = True)
                        print(output)
    return output

group(df,IDCTV,df_groups)

print(df_groups) 

1 个答案:

答案 0 :(得分:0)

我想你要改变

group(df,IDCTV,df_groups)

df_groups = group(df,IDCTV,df_groups)

现在您正在调用group函数并执行所有计算,但您并未将输出保存在任何位置。因此,当您运行print(df_groups)时,它会在您调用函数之前打印出它。