dazai
dazai

Reputation: 716

Drop rows with 'NoneType' and with strings: keep only numeric values

I have this dataset in which one of the columns has empty rows and some strings, whereas I only need to keep the numeric ones.

I have tried this for the strings:

df_3 = df_cor_inc[['Person ID','rt']]
df5 = df_3.to_csv('Documents/a.csv',index=False)
df5['rt'].apply(lambda x: pd.to_numeric(x, errors = 'coerce')).dropna()

But I get: AttributeError: 'NoneType' object has no attribute 'dropna'.

This doesn't work either because 'AttributeError: 'NoneType' object has no attribute 'rt':

df5[df5.rt.apply(lambda x: x.isnumeric())]

Same thing happens when I try to get rid of the empty rows, I get an error because I have 'NoneType'. How do I get rid of it so that I only keep the numeric values of that column and delete all the rows that don't have them?

This is how the data looks:

Person ID,rt
0,445   
0,445   
0,445   
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,Wait success  
1,  
1,  
1,  
1,  
1,  
1,1230  
1,1230  
1,1230  
1,1230  
1,1230  
1,1230  
1,1721  
1,1721  
1,1721  
1,1721  
1,1721  
1,1721  

Upvotes: 0

Views: 663

Answers (1)

jezrael
jezrael

Reputation: 862611

Problem here is ouput variable is set to None if write df_3 by to_csv to file.

df5 = df_3.to_csv('Documents/a.csv',index=False)

Solution is working only with df_3 like:

df_3 = df_cor_inc[['Person ID','rt']]
#no assign to df5
df_3.to_csv('Documents/a.csv',index=False)

#assigned converted values to numeric
df_3['rt'] = pd.to_numeric(df_3['rt'], errors = 'coerce')
#removed NaNs rows by rt column
df5 = df_3.dropna(subset=['rt'])

Upvotes: 1

Related Questions