Pandas- nan တန်ဖိုးများကို စာကြောင်းတစ်ခုဖြင့် အစားထိုးနည်း
ပန်ဒါဒေတာဘောင်တွင် NaN တန်ဖိုးများကို စာကြောင်းများဖြင့် အစားထိုးရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-
နည်းလမ်း 1- DataFrame တစ်လျှောက်လုံးတွင် NaN တန်ဖိုးများကို string တစ်ခုဖြင့် အစားထိုးပါ။
df. fillna ('', inplace= True )
နည်းလမ်း 2- တိကျသောကော်လံများတွင် ကြိုးတစ်ချောင်းဖြင့် NaN တန်ဖိုးများကို အစားထိုးပါ။
df[[' col1 ', ' col2 ']] = df[[' col1 ', ' col2 ']]. fillna ('')
နည်းလမ်း 3- ကော်လံတစ်ခုရှိ ကြိုးတစ်ချောင်းဖြင့် NaN တန်ဖိုးများကို အစားထိုးပါ။
df. col1 = df. col1 . fillna ('')
အောက်ပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-
import pandas as pd import numpy as np #create DataFrame with some NaN values df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], ' points ': [np.nan, 11, 7, 7, 8, 6, 14, 15], ' assists ': [5, np.nan, 7, 9, 12, 9, 9, 4], ' rebounds ': [11, 8, 10, np.nan, 6, 5, 9, np.nan]}) #view DataFrame df team points assists rebounds 0 A NaN 5.0 11.0 1 A 11.0 NaN 8.0 2 A 7.0 7.0 10.0 3 A 7.0 9.0 NaN 4 B 8.0 12.0 6.0 5 B 6.0 9.0 5.0 6 B 14.0 9.0 9.0 7 B 15.0 4.0 NaN
နည်းလမ်း 1- DataFrame တစ်လျှောက်လုံးတွင် NaN တန်ဖိုးများကို string တစ်ခုဖြင့် အစားထိုးပါ။
အောက်ပါကုဒ်သည် DataFrame တစ်ခုလုံးတွင် NaN တန်ဖိုးတစ်ခုစီကို အလွတ်စာကြောင်းဖြင့် အစားထိုးနည်းကို ပြသသည်-
#replace NaN values in all columns with empty string
df. fillna ('', inplace= True )
#view updated DataFrame
df
team points assists rebounds
0 A 5.0 11.0
1 A 11.0 8.0
2 A 7.0 7.0 10.0
3 A 7.0 9.0
4 B 8.0 12.0 6.0
5 B 6.0 9.0 5.0
6 B 14.0 9.0 9.0
7B 15.0 4.0
ကော်လံတစ်ခုစီရှိ NaN တန်ဖိုးတစ်ခုစီကို ဗလာစာကြောင်းတစ်ခုဖြင့် အစားထိုးထားသည်ကို သတိပြုပါ။
နည်းလမ်း 2- တိကျသောကော်လံများတွင် ကြိုးတစ်ချောင်းဖြင့် NaN တန်ဖိုးများကို အစားထိုးပါ။
အောက်ပါကုဒ်သည် တိကျသောစာကြောင်းတစ်ခုဖြင့် သီးခြားကော်လံများတွင် NaN တန်ဖိုးများကို မည်သို့အစားထိုးရမည်ကို ပြသသည်-
#replace NaN values in 'points' and 'rebounds' columns with 'none'
df[[' points ', ' rebounds ']] = df[[' points ', ' rebounds ']]. fillna (' none ')
#view updated DataFrame
df
team points assists rebounds
0 A none 5.0 11.0
1 A 11.0 NaN 8.0
2 A 7.0 7.0 10.0
3 A 7.0 9.0 none
4 B 8.0 12.0 6.0
5 B 6.0 9.0 5.0
6 B 14.0 9.0 9.0
7 B 15.0 4.0 none
“ points” နှင့် “ rebounds” ကော်လံများရှိ NaN တန်ဖိုးများကို string “ none” ဖြင့် အစားထိုးထားသော်လည်း “ assists” ကော်လံရှိ NaN တန်ဖိုးများသည် မပြောင်းလဲသေးပါ။
နည်းလမ်း 3- ကော်လံတစ်ခုရှိ ကြိုးတစ်ချောင်းဖြင့် NaN တန်ဖိုးများကို အစားထိုးပါ။
အောက်ပါကုဒ်သည် ကော်လံတစ်ခုတွင် NaN တန်ဖိုးများကို တိကျသောစာကြောင်းဖြင့် အစားထိုးနည်းကို ပြသသည်-
#replace NaN values in 'points' column with 'zero'
df. points = df. points . fillna (' zero ')
#view updated DataFrame
df
team points assists rebounds
0 To zero 5.0 11.0
1 A 11.0 NaN 8.0
2 A 7.0 7.0 10.0
3 A 7.0 9.0 NaN
4 B 8.0 12.0 6.0
5 B 6.0 9.0 5.0
6 B 14.0 9.0 9.0
7 B 15.0 4.0 NaN
“ points” ကော်လံရှိ NaN တန်ဖိုးကို string “ သုည” ဖြင့် အစားထိုးခဲ့သည်ကို သတိပြုပါ၊ သို့သော် “ assists” နှင့် “ rebounds” ကော်လံများတွင် NaN တန်ဖိုးများသည် မပြောင်းလဲပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas- အခြေအနေပေါ်အခြေခံ၍ ကော်လံတစ်ခုရှိ တန်ဖိုးများကို အစားထိုးနည်း
Pandas- NaN တန်ဖိုးများကို သုညဖြင့် အစားထိုးနည်း
Pandas- DataFrame တွင် ပျောက်ဆုံးနေသောတန်ဖိုးများကို မည်သို့ရေတွက်မည်နည်း။