ပြင်နည်း- .str accessor ကို string values များဖြင့်သာ သုံးနိုင်သည်။
Python ကိုအသုံးပြုရာတွင် သင်ကြုံတွေ့ရနိုင်သည့် အမှားတစ်ခုမှာ-
AttributeError : Can only use .str accessor with string values!
pandas DataFrame ၏ string ကော်လံတွင် ပုံစံတစ်ခုကို အစားထိုးရန် ကြိုးပမ်းသောအခါတွင် ဤအမှားသည် ပုံမှန်အားဖြင့် ဖြစ်ပေါ်တတ်သော်လည်း သင်လုပ်ဆောင်နေသော ကော်လံသည် အမှန်တကယ်တွင် စာကြောင်းမဟုတ်ပေ။
အောက်ဖော်ပြပါ ဥပမာသည် ဤအမှားကို လက်တွေ့တွင် မည်သို့ပြုပြင်ရမည်ကို ပြသထားသည်။
အမှားကို ဘယ်လိုပြန်ထုတ်မလဲ။
ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။
import pandas as pd
#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
' points ': [6.5, 7.8, 8.0, 9.0, 7.5, 3.4, 6.6, 6.8],
' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]})
#view DataFrame
df
team points assists rebounds
0 A 6.5 5 11
1 A 7.8 7 8
2 A 8.0 7 10
3 A 9.0 9 6
4 B 7.5 12 6
5 B 3.4 9 5
6 B 6.6 9 9
7 B 6.8 4 12
ယခု ကျွန်ုပ်တို့သည် “အမှတ်များ” ကော်လံရှိ ဒဿမနေရာကို နေရာလွတ်တစ်ခုဖြင့် အစားထိုးရန် ကြိုးစားသည်ဆိုပါစို့။
#attempt to replace decimal in "points" column with a blank
df[' points '] = df[' points ']. str . replace (' . ', '')
AttributeError : Can only use .str accessor with string values!
“ အမှတ်များ” ကော်လံသည် စာကြောင်းကော်လံမဟုတ်သောကြောင့် ကျွန်ုပ်တို့သည် အမှားအယွင်းတစ်ခုကို လက်ခံရရှိနေပါသည်။
အမှားကိုဘယ်လိုပြင်မလဲ။
ဤအမှားကိုဖြေရှင်းရန် အလွယ်ဆုံးနည်းလမ်းမှာ “ points” ကော်လံရှိ တန်ဖိုးများကို အစားထိုးရန် မကြိုးစားမီ .astype(str) လုပ်ဆောင်ချက်ကို အသုံးပြုရန်ဖြစ်သည်-
#replace decimal in "points" column with a blank
df[' points '] = df[' points ']. astype (str). str . replace (' . ', '')
#view updated DataFrame
df
team points assists rebounds
0 A 65 5 11
1 A 78 7 8
2 A 80 7 10
3 A 90 9 6
4 B 75 12 6
5 B 34 9 5
6 B 66 9 9
7 B 68 4 12
“ points” ကော်လံရှိ ဒဿမနေရာတိုင်းကို အစားထိုးထားပြီး၊ “ points” ကော်လံရှိ တန်ဖိုးများကို အစားထိုးရန် မကြိုးစားမီ .astype(str) လုပ်ဆောင်ချက်ကို အသုံးပြုထားသောကြောင့် ကျွန်ုပ်တို့သည် အမှားအယွင်းများကို လက်ခံရရှိခြင်းမရှိကြောင်း သတိပြုပါ။
မှတ်ချက် – replace() လုပ်ဆောင်ချက်အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာဖွေနိုင်ပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြင်ဆင်ရမည်ကို ရှင်းပြသည်-
Pandas တွင် KeyError ကိုဘယ်လိုပြင်မလဲ။
ပြင်ဆင်နည်း- ValueError- float NaN ကို int အဖြစ်သို့ ပြောင်းလဲ၍မရပါ။
ပြုပြင်နည်း- တန်ဖိုးအမှား- Operands များကို ပုံသဏ္ဍာန်များဖြင့် ထုတ်လွှင့်၍မရပါ။