helix
helix

Reputation: 21

labeling data in Pandas

I have a big dataframe. I want to classify my data like this:

if element > threshold:
    element = 1
elif element < -threshold:
    element = -1
else:
    element = 0

Trying to do it like this in pandas, but it obviously leaves out the range [-threshold,threshold]. Is there a way to use "else" on an entire column?

for col in data:
    data[col][data[col] > threshold] = 1
    data[col][data[col] < -threshold] = -1

Upvotes: 1

Views: 251

Answers (1)

piRSquared
piRSquared

Reputation: 294218

Use np.sign

np.sign(data - threshold)

Upvotes: 1

Related Questions