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?

7
  • Someone edited your question to fix the formatting, but he might not have gotten the indentation right. Please check it and fix errors. Commented Jan 21, 2020 at 1:20
  • return value probably shouldn't be in the loop Commented Jan 21, 2020 at 1:20
  • What is nps_list? Is that supposed to be the list of column names? Commented Jan 21, 2020 at 1:21
  • What is df[i]? There's no i variable outside the for loop. Commented Jan 21, 2020 at 1:27
  • nps_list is list. Commented Jan 21, 2020 at 1:31

1 Answer 1

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)`
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your help and I have one more question. If I replace value in new columns using list, How can I loop through the list?
Not sure I understand the question... You want to write the results to new columns instead of overwriting the old ones?
Yes, I want to write the results to new columns using list.
Updated answer above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.