0

I am using Pandas and I need to modify some columns (product columns) which are supposed to have numbers but some of the entries are blanks or alphanumeric characters. I want to convert all non numeric entries, (including blanks, and NaN) to 0. Here is my code (I am using spyder):

Tracker = tracker_master[['Name','Month','Year','Date','Owner',
                              'Account_Name','Opp','Proposed_Solution',
                              'Product1','Product2','Product3','Product4','Product5','Signed_Date','Stage','timestamp']]

Tracker_final = Tracker[(Tracker.Year==2018) & (Tracker.Month.isin(['April','May','June','July','August'])) & 
                             (Tracker.Stage.isin(['Inked','Approved']))]

Tracker_final[['Product1','Product2','Product3','Product4','Product5']].apply(pd.to_numeric, errors='coerce').fillna(0)

Tracker_final.to_excel(r'W:\mydrive\B\TrackerFinal.xlsx', index=False) 

When I try to run this code I still get the NaN for the Product columns. What am I doing wrong?

1 Answer 1

1

It appears you just need to assign back to a sub-dataframe of your selected columns:

cols = ['Product'+str(i) for i in range(1, 6)]

Tracker_final[cols] = Tracker_final[cols].apply(pd.to_numeric, errors='coerce').fillna(0)

If this does not work, please update your question to include sample data which replicates your problem.

Sign up to request clarification or add additional context in comments.

Comments

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.