Pandas: ValueError: cannot convert float NaN to integer

后端 未结 5 802
攒了一身酷
攒了一身酷 2020-12-03 00:49

I get ValueError: cannot convert float NaN to integer for following:

df = pandas.read_csv(\'zoom11.csv\')
df[[\'x\']] = df[[\'x\']].astype(i         


        
5条回答
  •  北海茫月
    2020-12-03 01:22

    For identifying NaN values use boolean indexing:

    print(df[df['x'].isnull()])
    

    Then for removing all non-numeric values use to_numeric with parameter errors='coerce' - to replace non-numeric values to NaNs:

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

    And for remove all rows with NaNs in column x use dropna:

    df = df.dropna(subset=['x'])
    

    Last convert values to ints:

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

提交回复
热议问题