设置新数据框列的值:
df.loc[df["Measure] == metric.label, "source_data_url"] = metric.source_data_url
Run Code Online (Sandbox Code Playgroud)
现在(从 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.
Run Code Online (Sandbox Code Playgroud)
Pandas文档讨论了如何解决 Series 的问题,但不清楚如何在分配新的 DataFrame 列时迭代地执行此操作(上面的行在指标循环中调用,它是发出警告的最终指标) 。如何才能做到这一点?