我有一个要添加新列“ being_narrative”的数据框。数据框信息如下所示:
数据框具有以下行索引:
我尝试使用此命令添加字符串类型的新列:
explain_instance_to_explain_df_sorted_top6['being_narrative'] = ""
但是,它会给出“设置为带有复制”警告:
_main __:1:SettingWithCopyWarning: 试图在DataFrame的切片副本上设置一个值。 尝试改用.loc [row_indexer,col_indexer] =值
此外,没有为该列分配新值
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'] = ""
但是再次得到警告,显然数据框存在问题: