“ValueError: Impossible de convertir Float Nan en entier” Réponses codées

Impossible de convertir Float Nan en entier

# 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)
Odd Oryx

ValueError: Impossible de convertir Float Nan en entier

# 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 \n",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 \n",df)
Gorgeous Gazelle

ValueError: Impossible de convertir Float Nan en entier

# 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 \n",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 \n",df)
Gorgeous Gazelle

ValueError: Impossible de convertir Float Nan en entier

# 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 \n",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 \n",df)
Gorgeous Gazelle

Réponses similaires à “ValueError: Impossible de convertir Float Nan en entier”

Questions similaires à “ValueError: Impossible de convertir Float Nan en entier”

Plus de réponses similaires à “ValueError: Impossible de convertir Float Nan en entier” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code