0

The below column 'Total' from my data frame has integers with extra .0 (1.0,2.0..) & Null values. Before exporting data to csv, when below line of code executes it is throwing the error : ValueError: invalid literal for int() with base 10: ''

df['Total'] = df['Total'].astype('Int64')

How to handle a column which has int & null values. At the same time if i remove above validation, Total output column coming as extra .0 (Ex: 11.0, 199.0, 33.0 etc). I want to export pure int/null to my csv. Any help is highly appreciated.

1 Answer 1

1

It seems there are some non numeric values, you can try to_numeric with errors='coerce' for convert this values to NaNs:

df['Total'] = pd.to_numeric(df['Total'], errors='coerce').astype('Int64')
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the reply : After above change, now i'm getting below error: raise ValueError('Cannot convert non-finite values (NA or inf) to ' ValueError: Cannot convert non-finite values (NA or inf) to integer
@RK. - I guess problem is some floats values like 32.4, how working df['Total'] = pd.to_numeric(df['Total'], errors='coerce').round().astype('Int64') for convert like 32.4 to 32 by round?
It has all int/null values only. integers like 32.0, 12.0, etc. No value after dot, we can trim it also.
@RK. - What is your pandas version? Because converting to integers is possible only in some 5 last version of pandas, pandas 0.24+

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.