본문 바로가기
파이썬

파이썬 Pandas : ValueError : float NaN을 정수로 변환 할 수 없습니다.

by º기록 2020. 10. 15.
반응형

ValueError : cannot convert float NaN to integer for following :

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

업데이트 : 주석 / 답변의 힌트를 사용하여 데이터를 정리했습니다.

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

 

해결 방법

 


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


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


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

마지막으로 값을 int 로 변환 :

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

 

참조 페이지 https://stackoverflow.com/questions/47333227

 

 

반응형

댓글