设置数据框新列的值:
df.loc[df["Measure] == metric.label, "source_data_url"] = metric.source_data_url
现在(截至Pandas版本2.1.0)会产生一条警告,
FutureWarning:
Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value ' metric_3' has dtype incompatible with float64, please explicitly cast to a compatible dtype
first.
Pandas文档讨论了如何解决Series中出现的这个问题,但对于在循环中迭代地为DataFrame分配新列时(如上一行代码在一个遍历metrics的循环中调用,并且是最后一次赋值时触发警告),该怎么做并不明确。应该如何处理这种情况呢?