首页 > 解决方案 > 为什么当列具有 NaN 值时我的循环停止?

问题描述

我正在遍历两个唯一列表并为每个产品/客户组合运行 OLS 回归模型。如果前两次迭代有数据,它将运行,但是一旦到达具有大部分或所有 nan 值的迭代,循环就会停止,并且只给我前一次迭代的结果。如果前一个是空的,我如何让我的循环继续到下一个产品/客户迭代?

谢谢!

       # Create Try and Except Clause:
    try:
        # Create empty formula list:
        formula_list = []
        # Create Empty Model List:
        model_list = []
        # Create Empty Data Frame:
        dataframe_final = pd.DataFrame()
        for g in geo_list:
            # Create Second Loop:
            for p in product_list:
                # Create Fitler DataFrame based on customer and product:
                df_filter = df[(df.Geography == g) & (df.Product == p)]
                # continue through function if dataframe is empty for a specific customer/product iteration:
                if df_filter.empty:
                   continue

                # Create a list of the numeric data types to log transform and transform dataframe:
                numerics = ["int16", "int32", "int64", "float16", "float32", "float64"]
                # Loop through the columns in the dataframe and take the log of any column that has any of the above stated data types:
                for c in [
                    c for c in df_filter.columns if df_filter[c].dtype in numerics
                ]:
                    df_filter[c] = np.log10(df_filter[c])
                    # Execute rename_function to get suffix "_Log" on desired columns:
                df_filter = rename_function(keep_same, df_filter)
                PED_Type(ped_type, df_filter)
                #df_filter = df_filter.fillna(0)
                # Create Model Formula:
                models = sm.OLS.from_formula(formula=formulas, data=df_filter).fit()
                # Append the models to the empty model_list:
                model_list.append(models)

标签: pythonpandasloopsregression

解决方案


推荐阅读