I have a large dataframe which looks as:
JavaScript
x
4
1
df1['A'].ix[1:3]
2
2017-01-01 02:00:00 [33, 34, 39]
3
2017-01-01 03:00:00 [3, 43, 9]
4
I want to replace each element greater than 9 with 11.
So, the desired output for above example is:
JavaScript
1
4
1
df1['A'].ix[1:3]
2
2017-01-01 02:00:00 [11, 11, 11]
3
2017-01-01 03:00:00 [3, 11, 9]
4
Edit:
My actual dataframe has about 20,000 rows and each row has list of size 2000.
Is there a way to use numpy.minimum
function for each row? I assume that it will be faster than list comprehension
method?
Advertisement
Answer
You can use apply
with list comprehension
:
JavaScript
1
6
1
df1['A'] = df1['A'].apply(lambda x: [y if y <= 9 else 11 for y in x])
2
print (df1)
3
A
4
2017-01-01 02:00:00 [11, 11, 11]
5
2017-01-01 03:00:00 [3, 11, 9]
6
Faster solution is first convert to numpy array
and then use numpy.where
:
JavaScript
1
11
11
1
a = np.array(df1['A'].values.tolist())
2
print (a)
3
[[33 34 39]
4
[ 3 43 9]]
5
6
df1['A'] = np.where(a > 9, 11, a).tolist()
7
print (df1)
8
A
9
2017-01-01 02:00:00 [11, 11, 11]
10
2017-01-01 03:00:00 [3, 11, 9]
11