user7146708
user7146708

Reputation: 197

Modify some columns of a dataframe with pandas

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?

Upvotes: 0

Views: 467

Answers (1)

jpp
jpp

Reputation: 164843

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.

Upvotes: 1

Related Questions