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?

theQman
  • 1,690
  • 6
  • 29
  • 52

1 Answers1

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
EdChum
  • 376,765
  • 198
  • 813
  • 562