Home > Blockchain >  Converted Object to Int but now all values are NaN using Jupyter pandas
Converted Object to Int but now all values are NaN using Jupyter pandas

Time:08-27

When i have entered my dataset and converted from object to int, all the values are changing to NaN.

This is my code:

iron_map = {'No': 1, 'Steady': 2, 'Down': 3, 'Up': 4}
df['iron'] = df['iron'].map(iron_map)
df['iron'] = df['iron'].astype(int)

or alternatively it happened on this one too:

df['number_hospital_visits'] = df['number_hospital_visits'].astype(str)

df = df[df['number_hospital_visits'].str.isnumeric()] - *I did this as some values were "?" so converted to string first then int*

df['number_hospital_visits'] = df['number_hospital_visits'].astype(int)

Anyone know how to fix this?

EDIT:

Have attached an example of the df data:

enter image description here

CodePudding user response:

I've tried replicating your issue, but your code works for me:

import pandas as pd

# Adding only three columns for simplicity
d = {'iron': ["No", "Steady", "Down", "Up", "Down"], 
     'diabetes': ["Yes", "No", "No", "Yes", "Yes"],
     'number_diagnoses': [1,1,1,1,1]
}
df = pd.DataFrame(data=d)

iron_map = {'No': 1, 'Steady': 2, 'Down': 3, 'Up': 4}
df['iron'] = df['iron'].map(iron_map)
df['iron'] = df['iron'].astype(int)

print(df)

Output:

   iron diabetes  number_diagnoses
0     1      Yes                 1
1     2       No                 1
2     3       No                 1
3     4      Yes                 1
4     3      Yes                 1

Do you still have the issue you are describing, executing this code?

  • Related