ပြင်ဆင်နည်း- valueerror- float nan ကို int သို့ ပြောင်းလဲ၍မရပါ။


ပန်ဒါများကို အသုံးပြုရာတွင် သင်ကြုံတွေ့ရနိုင်သည့် အမှားတစ်ခုမှာ-

 ValueError : cannot convert float NaN to integer

ကော်လံတွင် NaN တန်ဖိုးများပါရှိသောအခါ၊ float မှ ကိန်းပြည့်သို့ pandas DataFrame ရှိ ကော်လံတစ်ခုသို့ ပြောင်းရန် ကြိုးပမ်းသောအခါတွင် ဤအမှားဖြစ်ပေါ်ပါသည်။

အောက်ဖော်ပြပါ ဥပမာသည် ဤအမှားကို လက်တွေ့တွင် မည်သို့ပြုပြင်ရမည်ကို ပြသထားသည်။

အမှားကို ဘယ်လိုပြန်ထုတ်မလဲ။

ကျွန်ုပ်တို့သည် အောက်ပါ ပန်ဒါ DataFrame ကို ဖန်တီးသည်ဆိုပါစို့။

 import pandas as pd
import numpy as np

#createDataFrame
df = pd. DataFrame ({' points ': [25, 12, 15, 14, 19, 23, 25, 29],
                   ' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
                   ' rebounds ': [11, np. no , 10, 6, 5, np. no , 9, 12]})

#view DataFrame
df

        points assists rebounds
0 25 5 11
1 12 7 NaN
2 15 7 10
3 14 9 6
4 19 12 5
5 23 9 NaN
6 25 9 9
7 29 4 12

လက်ရှိတွင်၊ “ bounces” ကော်လံသည် “ float” ဒေတာအမျိုးအစားဖြစ်သည်။

 #print data type of 'rebounds' column
df[' rebounds ']. dtype

dtype('float64')

“ bounces” ကော်လံကို float မှ ကိန်းပြည့်သို့ ပြောင်းရန် ကြိုးစားနေသည်ဆိုပါစို့။

 #attempt to convert 'rebounds' column from float to integer
df[' rebounds '] = df[' rebounds ']. astype (int)

ValueError : cannot convert float NaN to integer 

“ bounces” ကော်လံရှိ NaN တန်ဖိုးများကို ကိန်းပြည့်တန်ဖိုးများအဖြစ်သို့ ပြောင်းလဲ၍မရသောကြောင့် ValueError တစ်ခုရရှိထားပါသည်။

အမှားကိုဘယ်လိုပြင်မလဲ။

ဤအမှားကိုပြင်ရန်နည်းလမ်းမှာ ကော်လံကို float မှ ကိန်းပြည့်အဖြစ်သို့မပြောင်းမီ NaN တန်ဖိုးများကို ကိုင်တွယ်ရန်ဖြစ်သည်။

NaN တန်ဖိုးများပါရှိသောအတန်းများကို ဦးစွာခွဲခြားသတ်မှတ်ရန် အောက်ပါကုဒ်ကို ကျွန်ုပ်တို့အသုံးပြုနိုင်သည်-

 #print rows in DataFrame that contain NaN in 'rebounds' column
print (df[df[' rebounds ']. isnull ()])

   points assists rebounds
1 12 7 NaN
5 23 9 NaN

ထို့နောက် ကျွန်ုပ်တို့သည် NaN တန်ဖိုးများဖြင့် အတန်းများကို ဖယ်ရှားနိုင်သည် သို့မဟုတ် ကော်လံကို float မှ ကိန်းပြည့်သို့ မပြောင်းမီ အခြားတန်ဖိုးတစ်ခုဖြင့် NaN တန်ဖိုးများကို အစားထိုးနိုင်သည်-

နည်းလမ်း 1- NaN တန်ဖိုးများဖြင့် အတန်းများကို ဖယ်ရှားပါ။

 #drop all rows with NaN values
df = df. dropna ()

#convert 'rebounds' column from float to integer
df[' rebounds '] = df[' rebounds ']. astype (int) 

#view updated DataFrame
df
	points assists rebounds
0 25 5 11
2 15 7 10
3 14 9 6
4 19 12 5
6 25 9 9
7 29 4 12

#view class of 'rebounds' column
df[' rebounds ']. dtype

dtype('int64')

နည်းလမ်း 2- NaN တန်ဖိုးများကို အစားထိုးပါ။

 #replace all NaN values with zeros
df[' rebounds '] = df[' rebounds ']. fillna ( 0 )

#convert 'rebounds' column from float to integer
df[' rebounds '] = df[' rebounds ']. astype (int) 

#view updated DataFrame
df

	points assists rebounds
0 25 5 11
1 12 7 0
2 15 7 10
3 14 9 6
4 19 12 5
5 23 9 0
6 25 9 9
7 29 4 12

#view class of 'rebounds' column
df[' rebounds ']. dtype

dtype('int64')

နည်းလမ်းနှစ်ခုစလုံးသည် ကျွန်ုပ်တို့အား ValueError ကို ရှောင်ရှားနိုင်ပြီး float ကော်လံကို ကိန်းပြည့်ကော်လံအဖြစ်သို့ အောင်မြင်စွာပြောင်းနိုင်သည်ကို သတိပြုပါ။

ထပ်လောင်းအရင်းအမြစ်များ

အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြင်ဆင်ရမည်ကို ရှင်းပြသည်-

ပြုပြင်နည်း- ကော်လံများ ထပ်နေသော်လည်း နောက်ဆက်တွဲကို သတ်မှတ်မထားပါ။
ပြုပြင်နည်း- Object ‘numpy.ndarray’ တွင် ‘append’ attribute မရှိပါ။
ပြင်ဆင်နည်း- scalar တန်ဖိုးများအားလုံးကို အသုံးပြုပါက၊ အညွှန်းတစ်ခုကို ကျော်သွားရန်လိုအပ်သည်။

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်