0

我有一个要添加新列“being_narrative”的数据框。数据框信息如下所示:

数据框信息

数据框具有以下行索引:

在此处输入图像描述

我尝试使用以下命令添加字符串类型的新列:

explain_instance_to_explain_df_sorted_top6['being_narrative'] = ""

但是,它给出了带有复制警告的设置:

_main__:1:SettingWithCopyWarning:试图在 DataFrame 的切片副本上设置一个值。尝试改用 .loc[row_indexer,col_indexer] = value

当我尝试在不同的数据帧上使用上述命令时,我没有收到此警告: 在此处输入图像描述

此外,没有为该列分配新值

explain_instance_to_explain_df_sorted_top6['being_narrative'] = ""
for index,row in explain_instance_to_explain_df_sorted_top6.iterrows() :
   row['being_narrative']= str(np.where( row.obs_instance_to_explain not in (0,1) and row.features not in ('NUM_CASES'),' being '+str(row.obs_instance_to_explain),''))

运行上述分配后,新列仍然为空: 在此处输入图像描述

但是,这可以单独工作,如下所示: 在此处输入图像描述

似乎问题出在数据框上。可能是什么问题呢?提前致谢!

更新:

尝试使用@Aryerez 的建议来使用 .loc 约定,首先删除列,然后使用:

explain_instance_to_explain_df_sorted_top6.loc[:, 'being_narrative'] = ""

但是再次收到警告,显然数据框存在问题:

在此处输入图像描述

4

0 回答 0