ပြင်ဆင်နည်း- scalar တန်ဖိုးများအားလုံးကို အသုံးပြုပါက၊ အညွှန်းတစ်ခုကို ကျော်သွားရန်လိုအပ်သည်။
ပန်ဒါများကို အသုံးပြုရာတွင် သင်ကြုံတွေ့ရနိုင်သည့် အမှားတစ်ခုမှာ-
ValueError: If using all scalar values, you must pass an index
scalar တန်ဖိုးများအားလုံးကို ကျော်ဖြတ်ခြင်းဖြင့် pandas DataFrame ကို ဖန်တီးရန် ကြိုးပမ်းသောအခါတွင် ဤအမှားသည် ဖြစ်ပေါ်လာပါသည်။
အောက်ဖော်ပြပါ ဥပမာသည် ဤအမှားကို လက်တွေ့တွင် မည်သို့ပြုပြင်ရမည်ကို ပြသထားသည်။
အမှားကို ဘယ်လိုပြန်ထုတ်မလဲ။
scalar တန်ဖိုးများစွာမှ pandas DataFrame တစ်ခုကို ဖန်တီးရန် ကြိုးစားနေသည်ဆိုပါစို့။
import pandas as pd #define scalar values a = 1 b = 2 c = 3 d = 4 #attempt to create DataFrame from scalar values df = pd. DataFrame ({' A ':a,' B ':b,' C ':c,' D ':d}) ValueError: If using all scalar values, you must pass an index
ကျွန်ုပ်တို့သည် DataFrame သို့ scalar တန်ဖိုးများကိုသာ ကျော်လွန်သွားသောကြောင့် အမှားအယွင်းတစ်ခုကို လက်ခံရရှိသော်လည်း အညွှန်းတစ်ခုကို မဖြတ်သန်းနိုင်ခဲ့ပါ။
အမှားကိုဘယ်လိုပြင်မလဲ။
ဤ error ကို ဖြေရှင်းရန် သင်သုံးနိုင်သော နည်းလမ်းသုံးမျိုး ရှိပါသည်။
နည်းလမ်း ၁
import pandas as pd
#define scalar values
a = 1
b = 2
c = 3
d = 4
#create DataFrame by transforming scalar values to list
df = pd. DataFrame ({' A ':[a], ' B ':[b], ' C ':[c], ' D ':[d]})
#view DataFrame
df
A B C D
0 1 2 3 4
Method 2: Scalar Values နှင့် Success Index ကို ဖြတ်ပါ။
import pandas as pd
#define scalar values
a = 1
b = 2
c = 3
d = 4
#create DataFrame by passing scalar values and passing index
df = pd. DataFrame ({' A ': a, ' B ': b, ' C ': c, ' D ': d}, index=[ 0 ])
#view DataFrame
df
A B C D
0 1 2 3 4
နည်းလမ်း 3- အဘိဓာန်တွင် scalar တန်ဖိုးများကို ထည့်ပါ။
import pandas as pd
#define scalar values
a = 1
b = 2
c = 3
d = 4
#define dictionary of scalar values
my_dict = {' A ':1, ' B ':2, ' C ':3, ' D ':4}
#create DataFrame by passing dictionary wrapped in a list
df = pd. DataFrame ([my_dict])
#view DataFrame
df
A B C D
0 1 2 3 4
နည်းလမ်းတစ်ခုစီသည် တူညီသော DataFrame ကို ထုတ်လုပ်ကြောင်း သတိပြုပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြင်ဆင်ရမည်ကို ရှင်းပြသည်-
ပြင်ဆင်နည်း- ပန်ဒါအမည်ရှိ မော်ဂျူးမရှိပါ။
ပြင်ဆင်နည်း- numpy ဟု အမည်ပေးထားသော သင်ခန်းစာမရှိပါ။
ပြုပြင်နည်း- ကော်လံများ ထပ်နေသော်လည်း နောက်ဆက်တွဲကို သတ်မှတ်မထားပါ။
ပြင်ဆင်နည်း- Pandas တွင် SettingWithCopyWarning