'Jupyter - python - changing Sting to INT
The data set had "deaths" as object and I need to convert it to the INTEGER. I try to use the formula from another thread and it doesn't seem to work, if anyone can assist on what am I doing wrong here, would appreciate.
Input: data.info() Output: data.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 1270 entries, 0 to 1271 Data columns (total 5 columns):
Column Non-Null Count Dtype
0 year 1270 non-null object 1 leading_cause 1270 non-null object 2 sex 1270 non-null object 3 race_ethnicity 1270 non-null object 4 deaths 1270 non-null object dtypes: object(5) memory usage: 59.5+ KB
Input: df = pd.DataFrame({'deaths':['50','30','28']}) print (df) df = pd.DataFrame({'deaths':['50','30','28']}) print (df) Output: deaths 0 50 1 30 2 28
Input: print (pd.to_numeric(df.deaths, errors='coerce')) Output: 0 50 1 30 2 28 Name: deaths, dtype: int64
Input: df.deaths = pd.to_numeric(df.deaths, errors='coerce').astype('Int64') print (df) Output: deaths 0 50 1 30 2 28
Input: data.info() Output: <class 'pandas.core.frame.DataFrame'> Int64Index: 1270 entries, 0 to 1271 Data columns (total 5 columns):
Column Non-Null Count Dtype
0 year 1270 non-null object 1 leading_cause 1270 non-null object 2 sex 1270 non-null object 3 race_ethnicity 1270 non-null object 4 deaths 1270 non-null object dtypes: object(5) memory usage: 59.5+ KB
Solution 1:[1]
If you have nulls (np.NaN) in the column it will not convert to int type. You need to deal with nulls first.
1 Either replace them with an int value:
df.deaths = df.deaths.fillna(0)
df.deaths = df.deaths.astype(int)
2 Or drop null values:
df = df[df.deaths.notna()]
df.deaths = df.deaths.astype(int)
3 Or (preferred) learn to live with them:
# make your other broken function to accept the nulls
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anton Frolov |
