andrecdazevedo
andrecdazevedo

Reputation: 43

Python: Conditional column based on the lenght of another column

I have a column with telephone numbers and I need to add another one that says which numbers may be correct based in their lenght. (Telephone number with more than 6 digits may be correct)

I've tried:

df['correct number?'] = np.where(len(df['telephone']) > 6, 'Yes', 'No')

But it returned Yes to all rows.

Upvotes: 1

Views: 41

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

len(df['telephone']) is exactly equal to len(df) (why?). You want:

df['correct number?'] = np.where(df['telephone'].str.len() > 6, 'Yes', 'No')

or

df['correct number?'] = np.where(df['telephone'].apply(len) > 6, 'Yes', 'No')

Upvotes: 1

Related Questions