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 :: combine dataframes with two matching columns 
Python :: np.percentile 
Python :: dockerfile for django project 
Python :: import argv python 
Python :: create log in python 
Python :: How to change values in a pandas DataFrame column based on a condition in Python 
Python :: check python version 
Python :: pandas select rows by multiple conditions 
Python :: python relative file path doesnt work 
Python :: flask blueprint static folder 
Python :: unique_together what is used of it in django 
Python :: tabula python pdf to csv 
Python :: python program to add two numbers using function 
Python :: python stop while loop after time 
Python :: Example of lambda function in python with list 
Python :: install fasttext python 
Python :: how to create 3 dimensional array in numpy 
Python :: kivy change window size 
Python :: python get the last element from the list 
Python :: reverse an array pyton 
Python :: how to create a virtual environment in anaconda 
Python :: view all columns in pandas dataframe 
Python :: python typing effect 
Python :: python list transpose 
Python :: run django localhost server 
Python :: flatten image python numpy 
Python :: pandas groupby mean round 
Python :: run code in python atom 
Python :: herencia python 
Python :: current date and time django template 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =