iterrows()有时有效,有时却无效

时间:2018-07-23 20:23:43

标签: python python-3.x pandas

也许我正在慢慢失去理智,但是过去我已经使用iterrows()了,但是现在我正尝试使用它来编辑行值,而某些行确实被更改,而其他行却没有。 When iterrows works When iterrows doesn't work

以下方法有效(产生了名为“ 1MM”,“ 3MM”和“ 5MM”的新列并正确标记):

for index, row in merged.iterrows():
    merged.at[index, '1MM'] = 'Yes' if row['Opportunity'] >=1 else 'No'
    merged.at[index, '3MM'] = 'Yes' if row['Opportunity'] >=3 else 'No'
    merged.at[index, '5MM'] = 'Yes' if row['Opportunity'] >=5 else 'No'

但是这不起作用(某些客户公司名称未更改)

for i, row in ff.iterrows():
    ff.at[i,'new'] = 'Raymond James' if re.search('Raymond James',row["Client Firm"]) else row['Client Firm']
    ff.at[i,'new'] = 'Cetera' if re.search('Cetera',row['Client Firm']) else row['Client Firm']
    ff.at[i,'new'] = 'LPL' if re.search('LPL',row['Client Firm']) else row['Client Firm']
    ff.at[i,'new'] = 'Wells Fargo' if re.search('Wells Fargo',row['Client Firm']) else row['Client Firm']

第二个数据帧看起来像这样:

pd.DataFrame({'Client Firm':['Cetera Financial Services','LPL Financial Advisors','Raymond James Advisors'],'AUM':['50','30','40']})

0 个答案:

没有答案