J Doe
J Doe

Reputation: 359

Dropping rows with character values

I have 4 columns of data:

yyyymmdd         hh       mm          ss
20140102         02       20          31
20140102         02       20          32
2014o102         02       20          34

The 3rd row has a non-numeric character and I want to get rid of the entire row (even if there's a character in any of the other columns). I have tried the following:

df['yyyymmdd'] = pd.to_numeric(df['yyyymmdd'], errors='coerce')    
df['hh'] = pd.to_numeric(df['hh'], errors='coerce')    
df['mm'] = pd.to_numeric(df['mm'], errors='coerce')    
df['ss'] = pd.to_numeric(df['ss'], errors='coerce')    
df.dropna()

But, it's not working

Upvotes: 1

Views: 54

Answers (1)

piRSquared
piRSquared

Reputation: 294328

Do it all at once

pd.to_numeric(df.stack(), 'coerce').unstack().dropna()

   yyyymmdd  hh  mm  ss
0  20140102   2  20  31
1  20140102   2  20  32

Upvotes: 1

Related Questions