2

i have below df

date              T1   T2  T3  T4
1-1-2010 00:10    20   -5  4   3
1-1-2010 00:20    85  -78  34  21
1-1-2010 00:30    -45  22  31  75 
1-1-2010 00:40    -6   5   7  -28 

I would like to replace negative value into zero from 1st(from T1 columns) columns onwards.

i tried below code:

df.iloc[:,1:].mask(df, 0)

but its also showing '0' values of date columns.

final output should be:

date              T1   T2  T3  T4
1-1-2010 00:10    20   0   4   3
1-1-2010 00:20    85   0   34  21
1-1-2010 00:30    0    22  31  75 
1-1-2010 00:40    0    5   7    0       
1
  • Welcome to StackOverflow! can you update the df like a list of values? Commented Oct 25, 2019 at 6:57

3 Answers 3

2

Use pandas.DataFrame.clip:

df.iloc[:, 1:] = df.iloc[:, 1:].clip(0)
print(df)

Output:

             date  T1  T2  T3  T4
0  1-1-2010 00:10  20   0   4   3
1  1-1-2010 00:20  85   0  34  21
2  1-1-2010 00:30   0  22  31  75
3  1-1-2010 00:40   0   5   7   0

Not only clip is faster than mask in your sample, but also in the larger dataset:

# Your sample -> 3x faster
%timeit df.iloc[:, 1:].clip(0)
# 1.74 ms ± 115 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit df.iloc[:,1:].mask(df.iloc[:,1:] < 0, 0)
# 5.25 ms ± 573 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# Large Sample -> 1,000,000 elements --> about 30x
large_df = pd.DataFrame(pd.np.random.randint(-5, 5, (1000, 1000)))

%timeit large_df.clip(0)
# 17.2 ms ± 2.44 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit large_df.mask(large_df< 0, 0)
# 498 ms ± 47 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Sign up to request clarification or add additional context in comments.

1 Comment

a smart way to use clip !
1

Try to replace df with df.iloc[:,1:] < 0

df.iloc[:,1:] = df.iloc[:,1:].mask(df.iloc[:,1:] < 0, 0)

Comments

0
df = df1.iloc[:,1:]
df = df.mask(df<0 , 0)

Hope this will Helps.!:)

1 Comment

it removes date column from df

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.