Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SettingWithCopyWarning:试图在数据帧切片的副本上设置值_Python_Python 3.x_Pandas - Fatal编程技术网

Python SettingWithCopyWarning:试图在数据帧切片的副本上设置值

Python SettingWithCopyWarning:试图在数据帧切片的副本上设置值,python,python-3.x,pandas,Python,Python 3.x,Pandas,provider\u df是一个数据帧,您想创建一个名为“party”的新列,但它没有给出您要查找的np。其中 for i in range(0,len(donor_df)): if donor_df['cand_nm'][i] == 'Obama, Barack': donor_df['Party'] = 'Democrat' else: donor_df['Party'] = 'Republican' 谢谢你,它成功了,但是你能告诉我为什么上

provider\u df
是一个数据帧,您想创建一个名为“party”的新列,但它没有给出您要查找的
np。其中

for i in range(0,len(donor_df)):
    if donor_df['cand_nm'][i] == 'Obama, Barack':
        donor_df['Party'] = 'Democrat'
    else:
        donor_df['Party'] = 'Republican'

谢谢你,它成功了,但是你能告诉我为什么上面写的程序不成功吗working@Ben.hardy
donor_df['cand_nm'].iloc[i]
我想你的索引不是从1到len(df)开始的
donor_df['Party']=np.where(donor_df['cand_nm'] == 'Obama, Barack','Democrat','Republican')