0

I'm trying to break out of the loop mentality:

df = pd.DataFrame(data = np.arange(1,11), columns=['x'])

This gives me a column of 10 items, 1 through 10. I would like to create a new column y such that y is 0 if x is smaller than 5, and equal to x otherwise. Here is what I did:

def f(x):
    if x < 5:
        return 0
    else:
        return x

col = df['x']
df['y'] = col.apply(f)

Is there a better way to do this?

0

1 Answer 1

2

Use where to conditionally assign a value where true and return a different value if false:

In [57]:

df['y'] = df['x'].where(df['x']>5, 0)
df
Out[57]:
    x   y
0   1   0
1   2   0
2   3   0
3   4   0
4   5   0
5   6   6
6   7   7
7   8   8
8   9   9
9  10  10
1
  • Just return that using np.where: np.where(df['x']>5, 'Bigger', 'Smaller')
    – EdChum
    Mar 24, 2015 at 22:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.