1

I want to replace column value for multiple columns by def function. If value > 8 = 100, if value > 6 = 0, if value < 7 = 0, if NaN = NaN

My data is below.

ID  MONTH   COUNTRY Brand   A1  A2  A3  A4  A5  A6  A7  A8  A9  A10
1   201906  USA Apple   10  7   10  0   NaN NaN NaN 10  NaN NaN
2   201906  USA Samsung 8   6   8   NaN NaN NaN NaN 9   NaN NaN
3   201906  USA Apple   10  7   10  NaN NaN 10  3   10  NaN NaN
4   201906  USA Samsung 9   5   10  NaN 1   NaN NaN NaN 7   4
5   201906  USA Apple   10  7   10  NaN NaN NaN NaN 10  NaN NaN

I tried below code, but not changed column value.

list = ['A1', 'A3', 'A4', 'A7', 'A10']
new_list = ['B1', 'B3', 'B4', 'B7', 'B10']

def f(x):
    for i in list:
        if x[i] > 8:
            value = 100
        elif x[i] > 6:
            value = 0
        elif x[i] < 7:
            value = -100
        else:
            value = np.nan
        return value

df[new_list] = df[list].apply(f, axis=1)

How can I this?

purplecollar
  • 176
  • 1
  • 14

1 Answers1

2

I'd suggest that instead of looping inside your function, move it outside and loop through the columns:

list = ['A1', 'A3', 'A4', 'A7', 'A10']

def f(x):
    if x > 8:
        value = 100
    elif x > 6:
        value = 0
    elif x < 7:
        value = -100
    else:
        value = np.nan
    return value

for i in list:
    df[i] = df[i].apply(f)

If you want to write your updated values to new columns, are two ways you could approach it:

Add a fixed prefix:

for i in list:
    df[i + '_updated'] = df[i].apply(f)

Or use a second list to define the new column names:

list_current = ['A1', 'A3', 'A4', 'A7', 'A10']
list_new = ['B1', 'B3', 'B4', 'B7', 'B10']

for i, j in zip(list_current, list_new):
    df[j] = df[i].apply(f)`
Brett Romero
  • 343
  • 2
  • 12