Home > OS >  convert dtype from object to float
convert dtype from object to float

Time:08-02

df = pd.read_csv('C:/data/graduate-employment-survey.csv', sep=',', encoding='latin-1')

I want to convert the dtype from object to float but I encounter a error, it says "could not convert string to float: 'na'". In my file there are 'na' value in it , so how do i solve this if df['employment_rate_overall'] = df['employment_rate_overall'].str.replace(',', '').astype(float) cant be used to convert?

CodePudding user response:

You can try:

pd.to_numeric(df['employment_rate_overall'], errors='coerce')

Demo:

values = ['1', '2.3', np.nan, 'a', '4', '5.6', 'na']
pd.to_numeric(values, errors='coerce')

Output:

array([1. , 2.3, nan, nan, 4. , 5.6, nan])

CodePudding user response:

Your problem is that the code is literally trying to convert the string "na" to a float, which is not going to happen. If "na" is the only non-numeric element you can just do:

df = df.replace("na", None)

and do your conversion to float after that

  • Related