Python Forum
Problems with converting to numeric
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with converting to numeric
#1
This code for converting the values to numeric is also removing characters, I believe.

Can someone advise on how to alter it so it keeps the characters, except for the first line of code that removes the last '0;, that can stay as is. Thank you.

Here is the Before and After values, something in the code has caused Mic/Result to drop most of the values in the new MIC column:

Mic/Result: MIC:
0.125 NaN
0.125 NaN
<=0.03125 0.03125
0.125 NaN
0.125 NaN

# Remove 0 at the end for MIC/RESULT .
results_df['MIC/RESULT']=np.where(results_df['WHO CODE']=='LMU', results_df['MIC/RESULT'].str.rstrip('.0'),results_df['MIC/RESULT'])

#Convert MIC/RESULT to numeric
results_df['MIC']=np.where(results_df['WHO CODE']=='LMU',results_df['MIC/RESULT'].str.split(' ').str[1],results_df['MIC/RESULT'].replace(' ',''))
results_df['MIC']=pd.to_numeric(results_df['MIC'])
buran write Apr-12-2023, 06:37 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Problems with converting to numeric - by EmBeck87 - Apr-12-2023, 05:14 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020