0

Maybe I'm slowly losing my mind but I've gotten iterrows() to work in the past but now I'm trying to it to edit row values using it and some rows do get changed while other don't.When iterrows works When iterrows doesn't work

The following worked (produced new columns called '1MM','3MM', and '5MM' and labeled correctly):

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'

But this didn't work (some Client Firm names didn't get changed):

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']

The second dataframe looks something like this:

pd.DataFrame({'Client Firm':['Cetera Financial Services','LPL Financial Advisors','Raymond James Advisors'],'AUM':['50','30','40']})
matchat
  • 5
  • 4
  • 1
    Please don't post code/errors/dataframes as images, post them as text – user3483203 Jul 23 '18 at 20:25
  • That doesn't seem to have anything to do with `iterrows`. – user2357112 Jul 23 '18 at 20:31
  • 1
    You *should* be using something like `df.col1.str.extract`. You should really post a sample dataframe – user3483203 Jul 23 '18 at 20:31
  • 1
    You are constantly reassigning values to those cells... definetely shouldn't be doing this. Post sample input/output so we can help you out – rafaelc Jul 23 '18 at 20:35
  • Possible duplicate of [Extract string if match the value in another list](https://stackoverflow.com/questions/51353928/extract-string-if-match-the-value-in-another-list) – user3483203 Jul 23 '18 at 20:38
  • Possible duplicate of [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Will Jul 23 '18 at 20:40
  • I want to keep every row in the ff dataframe, just want to rename the ones that contain certain key words (LPL, Cetera, etc). I see the difference between str.contains and str.extract, but str.extract would get rid of the other rows I do want to keep – matchat Jul 23 '18 at 21:32
  • In the Pandas docs, they recommend not changing values using iterrows(). Try using itertuples() – chrisfs Jul 24 '18 at 07:44

0 Answers0