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)
0

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.