-3

I am new in Python and Pandas. I worked with SAS. In SAS I can use IF statement with "Do; End;" to update values of several columns based on one condition.
I tried np.where() clause but it updates only one column. The "apply(function, ...)" also updates only one column. Positioning extra update statement inside the function body didn't help.

Suggestions?

  • 7
    can u show a sample of your df(input and output), and the code you tried? – Joe Aug 13 '18 at 15:33
  • I want to implement: if (): df['col1'] = .... ; df[col2 ] = ... ; etc. . I know who to do it for each column separately with apply or np.where() – Alexander Aug 13 '18 at 16:52

2 Answers2

0

You can select which columns you want to alter, then use .apply():

df = pd.DataFrame({'a': [1,2,3], 
                   'b':[4,5,6]})

    a   b
0   1   4
1   2   5
2   3   6


df[['a','b']].apply(lambda x: x+1)

    a   b
0   2   5
1   3   6
2   4   7

This link may help:

Mariah Akinbi
  • 386
  • 1
  • 5
  • 19
  • Thank you for pointing out that I can apply lambda to multiple columns. However, It is very simplistic case. I want to implement: if (): df['col1'] = .... ; df[col2 ] = ... ; etc. . Yours – Alexander Aug 13 '18 at 16:39
0

You could use:

for col in df:
    df[col] = np.where(df[col] == your_condition, value_if, value_else)

eg:

   a  b
0  0  2
1  2  0
2  1  1
3  2  0

for col in df:
    df[col] = np.where(df[col]==0,12, df[col])

Output:

   a   b
0 12   2
1  2  12
2  1   1
3  2  12

Or if you want apply the condition only on some columns, select them in the for loop:

for col in ['a','b']:

or just in this way:

df[['a','b']] = np.where(df[['a','b']]==0,12, df[['a','b']])
Joe
  • 12,057
  • 5
  • 39
  • 55