Pandas dataframe တွင် numpy array ကိုမည်သို့ထည့်မည်နည်း။
တစ်ခါတစ်ရံတွင် သင်သည် Pandas DataFrame သို့ ကော်လံအသစ်တစ်ခုအဖြစ် NumPy အခင်းအကျင်းကို ထည့်လိုပေမည်။
ကံကောင်းထောက်မစွာ၊ သင်သည် အောက်ပါ syntax ကို အသုံးပြု၍ ၎င်းကို အလွယ်တကူ ပြုလုပ်နိုင်သည်။
df[' new_column '] = array_name. tolist ()
ဤသင်ခန်းစာတွင် ဤ syntax ကိုလက်တွေ့အသုံးပြုခြင်း၏နမူနာအချို့ကိုပြသထားသည်။
ဥပမာ 1- DataFrame တွင် ကော်လံအသစ်အဖြစ် NumPy အခင်းအကျင်းတစ်ခုကို ထည့်ပါ။
အောက်ဖော်ပြပါကုဒ်သည် ဘတ်စကက်ဘောကစားသမားစာရင်းအင်းအချို့ကို ထိန်းထားရန်နှင့် “ blocks” ဟုတံဆိပ်တပ်ထားသော ကော်လံအသစ်အဖြစ် NumPy အခင်းအကျင်းကို ထည့်သွင်းရန် ပန်ဒါ DataFrame ကို မည်သို့ဖန်တီးရမည်ကို ပြသသည်-
import numpy as np import pandas as pd #create pandas DataFrame df = pd.DataFrame({' points ': [25, 12, 15, 14, 19, 23, 25, 29], ' assists ': [5, 7, 7, 9, 12, 9, 9, 4], ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]}) #create NumPy array for 'blocks' blocks = np. array ([2, 3, 1, 0, 2, 7, 8, 2]) #add 'blocks' array as new column in DataFrame df[' blocks '] = blocks. tolist () #display the DataFrame print(df) points assists rebounds blocks 0 25 5 11 2 1 12 7 8 3 2 15 7 10 1 3 14 9 6 0 4 19 12 6 2 5 23 9 5 7 6 25 9 9 8 7 29 4 12 2
ယခု DataFrame အသစ်တွင် Blocks ခေါင်းစဉ်တပ်ထားသော နောက်ထပ်ကော်လံတစ်ခု ရှိနေကြောင်း သတိပြုပါ။
ဥပမာ 2- DataFrame တွင် NumPy Matrix ကို ကော်လံအသစ်အဖြစ် ထည့်ပါ။
အောက်ဖော်ပြပါကုဒ်သည် ဘတ်စကက်ဘောကစားသမားစာရင်းအင်းအချို့ကို ထိန်းထားရန်နှင့် “ blocks” ဟုတံဆိပ်တပ်ထားသော ကော်လံအသစ်အဖြစ် NumPy အခင်းအကျင်းကို ထည့်သွင်းရန် ပန်ဒါ DataFrame ကို မည်သို့ဖန်တီးရမည်ကို ပြသသည်-
import numpy as np import pandas as pd #create pandas DataFrame df = pd.DataFrame({' points ': [25, 12, 15, 14, 19, 23 #create NumPy matrix mat = np.matrix([[2, 3], [1, 0], [2, 7], [8, 2], [3, 4], [7, 7], [7, 5], [6, 3]]) #add NumPy matrix as new columns in DataFrame df_new = pd. concat ([df, pd.DataFrame(mat)], axis= 1 ) #display new DataFrame print(df_new) points assists rebounds 0 1 0 25 5 11 2 3 1 12 7 8 1 0 2 15 7 10 2 7 3 14 9 6 8 2 4 19 12 6 3 4 5 23 9 5 7 7 6 25 9 9 7 5 7 29 4 12 6 3
DataFrame တွင်ကျွန်ုပ်တို့ထည့်လိုက်သော matrix ၏ကော်လံအမည်များကို 0 နှင့် 1 ၏ ပုံသေကော်လံအမည်များပေးထားကြောင်း သတိပြုပါ။
df.columns လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ဤကော်လံများကို အလွယ်တကူ အမည်ပြောင်းနိုင်သည်-
#rename columns
df_new. columns = ['pts', 'ast', 'rebs', 'new1', 'new2']
#display DataFrame
print(df_new)
pts ast rebs new1 new2
0 25 5 11 2 3
1 12 7 8 1 0
2 15 7 10 2 7
3 14 9 6 8 2
4 19 12 6 3 4
5 23 9 5 7 7
6 25 9 9 7 5
7 29 4 12 6 3
ထပ်လောင်းအရင်းအမြစ်များ
Pandas DataFrames မျိုးစုံကို ဘယ်လိုစုမလဲ။
အညွှန်းတွင် Pandas DataFrames နှစ်ခုကို ပေါင်းစပ်နည်း
Pandas DataFrame ကို NumPy array သို့ ဘယ်လိုပြောင်းမလဲ။
Pandas တွင် ကော်လံအမည်ပြောင်းနည်း