RustyShackleford
RustyShackleford

Reputation: 3667

How to find string only if one condition is met and drop value from cell in dataframe but not the row itself?

I have a df that looks like this:

df:

col1    col2    
28      24 and 24
11      .1 for .1
3         43

I want to create logic that I can apply to a list of columns (not all the columns in the dataframe) where if the cell contains both integer and string the particular value in the cell gets replaced with empty string, but not drop the entire row.

new df should look like this:

col1    col2    
28      
11      
3         43

how would I do this?

Upvotes: 1

Views: 28

Answers (1)

BENY
BENY

Reputation: 323226

Using to_numeric

l=['col2']
df[l]=df[l].apply(pd.to_numeric,errors='coerce').fillna('')
df
Out[32]: 
   col1 col2
0    28     
1    11     
2     3   43

Upvotes: 1

Related Questions