Pandas apply() ကို ဘယ်လိုသုံးမလဲ။


Pandas Apply() လုပ်ဆောင်ချက်ကို ပန်ဒါ DataFrame ၏ အတန်းများ သို့မဟုတ် ကော်လံများတွင် လုပ်ဆောင်ချက်တစ်ခုကို အသုံးချရန် အသုံးပြုနိုင်သည်။

ဤလုပ်ဆောင်ချက်သည် အစားထိုးအငြင်းပွားမှုကို ပေးဆောင်သည့် drop() နှင့် အစားထိုး() ကဲ့သို့သော အခြားလုပ်ဆောင်ချက်များနှင့် ကွဲပြားသည်-

 df. drop ([' column1 '], inplace= True )

df. rename ({' old_column ': ' new_column '}, inplace= True )

apply() function တွင် inplace argument မပါသောကြောင့် DataFrame နေရာတွင် inplace အဖြစ်ပြောင်းလဲရန် အောက်ပါ syntax ကို အသုံးပြုရပါမည်။

 df = df. apply ( lambda x: x* 2 )

အောက်ဖော်ပြပါနမူနာများသည် အောက်ပါ pandas DataFrame နှင့် လက်တွေ့တွင် ဤ syntax ကိုမည်သို့အသုံးပြုရမည်ကို ပြသသည် ။

 import pandas as pd

#createDataFrame
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]})

#view DataFrame
df

        points assists rebounds
0 25 5 11
1 12 7 8
2 15 7 10
3 14 9 6
4 19 12 6
5 23 9 5
6 25 9 9
7 29 4 12

ဥပမာ 1- ကော်လံတစ်ခုအတွက် apply() ကိုသုံးပါ။

အောက်ဖော်ပြပါ ကုဒ်သည် ဒေတာဘောင်ကော်လံကို နေရာ၌ ပြောင်းလဲရန် apply() ကို အသုံးပြုပုံကို သရုပ်ပြသည်-

 #multiply all values in 'points' column by 2 in place
df. loc [:, ' points '] = df. points . apply ( lambda x: x* 2 )

#view updated DataFrame
df

points assists rebounds
0 50 5 11
1 24 7 8
2 30 7 10
3 28 9 6
4 38 12 6
5 46 9 5
6 50 9 9
7 58 4 12

ဥပမာ 2- ကော်လံများစွာအတွက် နေရာတွင် apply() ကိုသုံးပါ။

အောက်ပါကုဒ်သည် ဒေတာဘောင်ကော်လံများစွာကို နေရာတွင် ပြောင်းလဲရန် apply() ကို အသုံးပြုပုံကို သရုပ်ပြသည်-

 multiply all values in 'points' and 'rebounds' column by 2 in place
df[[' points ', ' rebounds ']] = df[[' points ', ' rebounds ']]. apply ( lambda x: x* 2 )

#view updated DataFrame
df

	points assists rebounds
0 50 5 22
1 24 7 16
2 30 7 20
3 28 9 12
4 38 12 12
5 46 9 10
6 50 9 18
7 58 4 24

ဥပမာ 3- ကော်လံအားလုံးအတွက် apply() ကိုသုံးပါ။

အောက်ပါကုဒ်သည် ဒေတာဘောင်ရှိ ကော်လံအားလုံးကို ပြောင်းလဲရန်အတွက် apply() ကို အသုံးပြုပုံကို ပြသသည်-

 #multiply values in all columns by 2
df = df. apply ( lambda x: x* 2 )

#view updated DataFrame
df

	points assists rebounds
0 50 10 22
1 24 14 16
2 30 14 20
3 28 18 12
4 38 24 12
5 46 18 10
6 50 18 18
7 58 8 24

ထပ်လောင်းအရင်းအမြစ်များ

အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-

Pandas ရှိ ကော်လံပေါင်းကို တွက်နည်း
Pandas ရှိ ကော်လံများ၏ ပျမ်းမျှအား တွက်ချက်နည်း
Pandas ရှိ ကော်လံများ၏ အမြင့်ဆုံးတန်ဖိုးကို မည်သို့ရှာမည်နည်း။

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်