对Pandas中的数据框中的值施加阈值

时间:2014-12-05 16:40:09

标签: pandas

我有以下代码:

t = 12
s = numpy.array(df.Array.tolist())
s[s<t] = 0
thresh = numpy.where(s>0, s-t, 0)
df['NewArray'] = list(thresh)

虽然它有效,但肯定必须有更多类似熊猫的方式。

修改
df.Array.head()看起来像这样:

0    [0.771511552006, 0.771515476223, 0.77143569165...
1    [3.66720695274, 3.66722560562, 3.66684636758, ...
2    [2.3047433839, 2.30475510675, 2.30451676559, 2...
3    [0.999991522708, 0.999996609066, 0.99989319662...
4    [1.11132718786, 1.11133284052, 0.999679589875,...
Name: Array, dtype: object

1 个答案:

答案 0 :(得分:2)

IIUC您可以简单地减去并使用clip_lower

In [29]: df["NewArray"] = (df["Array"] - 12).clip_lower(0)

In [30]: df
Out[30]: 
   Array  NewArray
0     10         0
1     11         0
2     12         0
3     13         1
4     14         2