我有一个要添加新列“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'] = ""
但是再次收到警告,显然数据框存在问题: