0

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  
2
  • What is code before? Seems df5 is Nonetype, not DataFrame Commented Jul 9, 2021 at 12:50
  • @jezrael oh, I added it. It comes from another dataframe and then I selected only two columns and saved them into a csv Commented Jul 9, 2021 at 12:51

1 Answer 1

1

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'])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I understand the part about the file but I still get some errors when it comes to dropping the rows. With the first option: pandas.core.base.DataError: No numeric types to aggregate. With the second option: AttributeError: 'float' object has no attribute 'isnumeric'
@AnuragDabas - I copy it from OP, you are right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.