python - 为什么当列具有 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)
解决方案
推荐阅读
- python-3.x - 使用 Robot Framework 时如何访问类中的方法?
- c - 当 printf 可以自行打印时,为什么在 printf 之后使用 fflush?
- c++ - C ++允许用户声明结构对象的最佳方法?
- docker - Nodeman 不使用 Docker 刷新
- android - 如何使用生成的客户端库在 Android 中取消谷歌云端点请求?
- vba - 尝试在 Word VBA 中获取文本框文本时出错
- c++ - 无法使用继承创建参数化构造函数,“不是非静态数据成员或类的基类”
- vue.js - 如何修改 Vue 堆叠 b 表的样式?
- javascript - 笔记标记进度条上的垂直线
- java - 为什么我的代码没有准确地给我反向号码