0

couple hours into this without success...

A simplified version of my dataframe looks like this:

     Name   A   B
0    Alex  20  10
1    Alex  10   5
2     Bob -12   4
3  Clarke  13  -3

Need to get to this:

     Name   A   B
0    Alex  20  10
1    Alex  10   5
2     Bob   0   4
3  Clarke  13   0

Using .loc for all rows and the subset columns of interest does not work:

from __future__ import print_function
import pandas as pd
import numpy as np
data = [['Alex',20, 10],['Alex',10, 5],['Bob',-12, 4],['Clarke',13, -3]]
df = pd.DataFrame(data,columns=['Name','A', 'B'])
colNames = df[['A','B']].columns.tolist()

print (df)

df[df.loc[:, colNames] < 0] = 0

print (df)

1 Answers1

0

You can use clip:

colNames = ['A', 'B']
df.loc[:, colNames] = df.loc[:, colNames].clip(lower=0)

print(df)

Output

     Name   A   B
0    Alex  20  10
1    Alex  10   5
2     Bob   0   4
3  Clarke  13   0
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76