0

I can't figure how to iterate a dataframe row by row and update cell values based on conditions from other cells. If a partial string is found in one cell and if simultaneously another cell has a specific value then I should add a specific value to a cell

Have tried also:

for i, row in df3.iterrows():
    codmarime = ''
    if df3['20755' in df3['ITEM CODE'][i]] and df3['24' in df3['TG'][i]]: 
       codmarime= '002'
       df3['SKU'][i] = '20'+str(df3['ITEM CODE'][i])+str(df3['COLOR CODE'][i])+codmarime


for i, row in df3.iterrows():
    codmarime = '' 
    if df3['ITEM CODE'].str.contains("20755") and df3['TG'].str.contains("24"): 
       codmarime= '002'
       df3['SKU'][i] = '20'+str(df3['ITEM CODE'][i])+str(df3['COLOR CODE'][i])+codmarime
    elif df3['ITEM CODE'].str.contains("20755") and df3['TG'].str.contains("25"): 
       codmarime= '003'
       df3['SKU'][i] = '20'+str(df3['ITEM CODE'][i])+str(df3['COLOR CODE'][i])+codmarime

Output

> ValueError    Traceback (most recent call last)
> 
> ----> 6     if df3['ITEM CODE'].str.contains("20755") and df3['TG'].str.contains("24"):
PaulTgMs
  • 33
  • 6
  • 2
    it will be helpful if you can add a data(sample) and expected output by referring [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) . Also use `&` in boolean conditions not `and` – anky Apr 13 '19 at 12:09
  • Please correctly format your output. – S.S. Anne Apr 13 '19 at 14:29

1 Answers1

1

Your code contains e.g. for i, row in df3.iterrows(): loop. Inside this loop row refers to the current row.

So instead of e.g. df3['SKU'][i] you should use row['SKU'] or even row.SKU.

Take analogous approach to other cases of read/write from columns of the current row.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41