izmir = pd.read_excel(filepath)
izmir_lim = izmir[[‘Gender’,’Age’,’MC_OLD_M>=60’,’MC_OLD_F>=60’,
‘MC_OLD_M>18’,’MC_OLD_F>18’,’MC_OLD_18>M>5’,
‘MC_OLD_18>F>5’,’MC_OLD_M_Child<5’,’MC_OLD_F_Child<5’,
‘MC_OLD_M>0<=1’,’MC_OLD_F>0<=1’,’Date to Delivery’,
‘Date to insert’,’Date of Entery’]]
izmir_lim是的视图/副本
izmir。随后,您尝试分配给它。这就是引发错误的原因。使用此代替:
izmir_lim = izmir[['Gender','Age','MC_OLD_M>=60','MC_OLD_F>=60', 'MC_OLD_M>18','MC_OLD_F>18','MC_OLD_18>M>5', 'MC_OLD_18>F>5','MC_OLD_M_Child<5','MC_OLD_F_Child<5', 'MC_OLD_M>0<=1','MC_OLD_F>0<=1','Date to Delivery', 'Date to insert','Date of Entery']].copy()
每当您以下列方式从另一个“创建”新数据框时:
new_df = old_df[list_of_columns_names]
new_df它的
is_copy属性将具有真实值。当您尝试分配给它时,熊猫会抛出
SettingWithCopyWarning。
new_df.iloc[0, 0] = 1 # Should throw an error
您可以通过多种方式克服这一点。
选项1选项2(如@ayhan在评论中建议)new_df = old_df[list_of_columns_names].copy()
选项#3new_df = old_df[list_of_columns_names]new_df.is_copy = None
new_df = old_df.loc[:, list_of_columns_names]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)