Pandas dataframe ကို စာသားဖိုင်သို့ တင်ပို့နည်း
သင်သည် pandas DataFrame ကို စာသားဖိုင်သို့ တင်ပို့ရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်ပါသည်။
#specify path for export path = r' c:\data_folder\my_data.txt ' #export DataFrame to text file with open (path, ' a ') as f: df_string = df. to_string (header= False , index= False ) f. write (df_string)
header=False argument သည် pandas များကို text file တွင် header row မထည့်ရန် နှင့် index=False သည် pandas များကို text file တွင် အညွှန်းကော်လံ မပါဝင်စေရန် ပြောထားသည်။
စာသားဖိုင်တွင် ခေါင်းစီးအတန်း သို့မဟုတ် အညွှန်းကော်လံကို ထည့်သွင်းလိုပါက ဤအငြင်းအခုံများကို ချန်လှပ်ထားနိုင်ပါသည်။
အောက်ဖော်ပြပါ ဥပမာသည် လက်တွေ့တွင် pandas DataFrame ကို စာသားဖိုင်တစ်ခုသို့ တင်ပို့ရန် ဤ syntax ကိုအသုံးပြုပုံကို ပြသထားသည်။
ဥပမာ- Pandas DataFrame ကို စာသားဖိုင်တစ်ခုသို့ တင်ပို့ပါ။
အမျိုးမျိုးသော ဘတ်စကတ်ဘောကစားသမားများအကြောင်း အချက်အလက်ပါရှိသော အောက်ပါပန်ဒါ DataFrame ရှိသည်ဆိုပါစို့။
import pandas as pd #createDataFrame df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], ' points ': [18, 22, 19, 14, 14, 11, 20, 28], ' assists ': [5, 7, 7, 9, 12, 9, 9, 4], ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print (df) team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7:28 4 12
ဤ DataFrame ကို basketball_data.txt ဟုခေါ်သော စာသားဖိုင်သို့ တင်ပို့ရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်ပါသည်။
#specify path for export path = r' c:\data_folder\basketball_data.txt ' #export DataFrame to text file with open (path, ' a ') as f: df_string = df. to_string (header= False , index= False ) f. write (df_string)
ကျွန်ုပ် ဤဖိုင်ကို ထုတ်ယူသည့် ဖိုင်တွဲသို့ သွားပါက၊ ကျွန်ုပ်သည် စာသားဖိုင်ကို ကြည့်ရှုနိုင်သည်-
စာသားဖိုင်ရှိ တန်ဖိုးများသည် pandas DataFrame ရှိ တန်ဖိုးများနှင့် ကိုက်ညီပါသည်။
ကျွန်ုပ်တို့သတ်မှတ်ထားသည့်အတိုင်း ခေါင်းစီးအတန်းနှင့် အညွှန်းကော်လံနှစ်ခုလုံးကို DataFrame မှ ဖယ်ရှားလိုက်ကြောင်း သတိပြုပါ။
စာသားဖိုင်တွင် ခေါင်းစီးအတန်းနှင့် အညွှန်းကော်လံကို ထားရှိလိုပါက၊ အောက်ပါ syntax ကို အသုံးပြုနိုင်ပါသည်။
#specify path for export path = r' c:\data_folder\basketball_data.txt ' #export DataFrame to text file (keep header row and index column) with open (path, ' a ') as f: df_string = df. to_string () f. write (df_string)
ကျွန်ုပ် ဤဖိုင်ကို ထုတ်ယူသည့် ဖိုင်တွဲသို့ သွားပါက၊ ကျွန်ုပ်သည် စာသားဖိုင်ကို ကြည့်ရှုနိုင်သည်-
ခေါင်းစီးအတန်းနှင့် အညွှန်းကော်လံနှစ်ခုလုံးကို စာသားဖိုင်တွင် ထည့်သွင်းထားကြောင်း သတိပြုပါ။
ဆက်စပ်- Python တွင် “with open” ကို အသုံးပြုနည်း
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas DataFrame ကို CSV သို့ တင်ပို့နည်း
Pandas DataFrame ကို Excel သို့ တင်ပို့နည်း
Pandas DataFrame ကို JSON သို့ တင်ပို့နည်း