I would like to know if there is someway of replacing all DataFrame negative numbers by zeros?
Advertisement
Answer
If all your columns are numeric, you can use boolean indexing:
JavaScript
x
20
20
1
In [1]: import pandas as pd
2
3
In [2]: df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1]})
4
5
In [3]: df
6
Out[3]:
7
a b
8
0 0 -3
9
1 -1 2
10
2 2 1
11
12
In [4]: df[df < 0] = 0
13
14
In [5]: df
15
Out[5]:
16
a b
17
0 0 0
18
1 0 2
19
2 2 1
20
For the more general case, this answer shows the private method _get_numeric_data
:
JavaScript
1
23
23
1
In [1]: import pandas as pd
2
3
In [2]: df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1],
4
'c': ['foo', 'goo', 'bar']})
5
6
In [3]: df
7
Out[3]:
8
a b c
9
0 0 -3 foo
10
1 -1 2 goo
11
2 2 1 bar
12
13
In [4]: num = df._get_numeric_data()
14
15
In [5]: num[num < 0] = 0
16
17
In [6]: df
18
Out[6]:
19
a b c
20
0 0 0 foo
21
1 0 2 goo
22
2 2 1 bar
23
With timedelta
type, boolean indexing seems to work on separate columns, but not on the whole dataframe. So you can do:
JavaScript
1
23
23
1
In [1]: import pandas as pd
2
3
In [2]: df = pd.DataFrame({'a': pd.to_timedelta([0, -1, 2], 'd'),
4
'b': pd.to_timedelta([-3, 2, 1], 'd')}) :
5
6
In [3]: df
7
Out[3]:
8
a b
9
0 0 days -3 days
10
1 -1 days 2 days
11
2 2 days 1 days
12
13
In [4]: for k, v in df.iteritems():
14
v[v < 0] = 0 :
15
:
16
17
In [5]: df
18
Out[5]:
19
a b
20
0 0 days 0 days
21
1 0 days 2 days
22
2 2 days 1 days
23
Update: comparison with a pd.Timedelta
works on the whole DataFrame:
JavaScript
1
14
14
1
In [1]: import pandas as pd
2
3
In [2]: df = pd.DataFrame({'a': pd.to_timedelta([0, -1, 2], 'd'),
4
'b': pd.to_timedelta([-3, 2, 1], 'd')}) :
5
6
In [3]: df[df < pd.Timedelta(0)] = 0
7
8
In [4]: df
9
Out[4]:
10
a b
11
0 0 days 0 days
12
1 0 days 2 days
13
2 2 days 1 days
14