Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

cannot convert float nan to integer

# x contained NaN
df = df[~df['x'].isnull()]

# Y contained some other garbage, so null check was not enough
df = df[df['y'].str.isnumeric()]

# final conversion now worked
df[['x']] = df[['x']].astype(int)
df[['y']] = df[['y']].astype(int)
Comment

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)

# replace the NaN values for specific column
df['price'] = df['price'].replace(np.nan, 0)

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

print("After conversion 
",df)
Comment

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)

# drop the rows which has NaN
df = df.dropna()

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

print("After conversion 
",df)
Comment

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)
Comment

PREVIOUS NEXT
Code Example
Python :: IQR to remove outlier 
Python :: python stacked bar chart from dataframe 
Python :: python list deep copy 
Python :: display pandas dataframe with border 
Python :: python singleton 
Python :: namedtuple python 
Python :: sklearn train test split 
Python :: keras normalize 
Python :: os.startfile 
Python :: how to loop through pages of pdf using python 
Python :: similarity index in python 
Python :: break line in string python 
Python :: Get a list of categories of categorical variable (Python Pandas) 
Python :: python turtle module 
Python :: concatenation of array in python 
Python :: abstract class python 
Python :: streamlit sidebar width 
Python :: move column in pandas dataframe 
Python :: reading the JSON from a JSON object 
Python :: python zip folder and subfolders 
Python :: return mean of df as dataframe 
Python :: re date python 
Python :: python 3 tkinter treeview example 
Python :: request download file 
Python :: python list contains 
Python :: labelimg yolo save format 
Python :: python for dummies 
Python :: how to hide tkinter window 
Python :: python is not clickable at point (434, 682). Other element would receive the click: 
Python :: gyp err! stack error: command failed: c:python39python.exe -c import sys; print "%s.%s.%s" % sys.version_info[:3]; 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =