Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

ValueError: cannot convert float NaN to integer

# import pandas library
import numpy as np
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'Antivirus': ['Windows Defender', 'AVG Antivirus', 'Mcafee Antivirus', 'Kaspersky Security', 'Norton Antivirus'],
                   'quantity': [10, 4, 8, 3, 5],
                   'price': [23.55, np.nan, 32.78, 33.0, np.nan]
                   })
print("Before conversion 
",df)
print("Data type of Price column is",df['price'].dtype)

# fill the NaN values with 0
df = df.fillna(0)

#attempt to convert 'price' column from float to integer
df['price'] = df['price'].astype(int)

print("After conversion 
",df)
Source by itsmycode.com #
 
PREVIOUS NEXT
Tagged: #convert #float #NaN #integer
ADD COMMENT
Topic
Name
6+2 =