向熊猫数据框添加空字符列很奇怪

我有一个要添加新列“ 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'] = ""

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

向熊猫数据框添加空字符列很奇怪

ilovegina 回答:向熊猫数据框添加空字符列很奇怪

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3146078.html

大家都在问