Pandas တွင် ကော်လံနှစ်ခုကို ပွားနည်း- ဥပမာများဖြင့်


Pandas DataFrame တွင် ကော်လံနှစ်ခုကို ပွားရန် အောက်ပါနည်းလမ်းများကို သင်သုံးနိုင်သည်။

နည်းလမ်း 1- ကော်လံနှစ်ခုကို မြှောက်ပါ။

 df[' new_column '] = df. column1 * df. column2

နည်းလမ်း 2- အခြေအနေပေါ်မူတည်၍ ကော်လံနှစ်ခုကို ပွားပါ။

 new_column = df. column1 * df. column2

#update values based on condition
df[' new_column '] = new_column. where (df. column2 == ' value1 ', other= 0 )

အောက်ဖော်ပြပါ ဥပမာများသည် နည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးချနည်းကို ပြသထားသည်။

ဥပမာ 1- ကော်လံနှစ်ခုကို မြှောက်ပါ။

ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' price ': [22, 20, 25, 30, 4, 8, 12, 10],
                   ' amount ': [3, 1, 3, 3, 2, 4, 3, 5]})

#view DataFrame
print (df)

   price amount
0 22 3
1 20 1
2 25 3
3 30 3
4 4 2
5 8 4
6 12 3
7 10 5

စျေးနှုန်း နှင့် ပမာဏ ကော်လံများကို မြှောက်ပြီး ဝင်ငွေ ဟုခေါ်သော ကော်လံအသစ်တစ်ခုကို ဖန်တီးရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်သည်။

 #multiply price and amount columns
df[' revenue '] = df. price * df. amount

#view updated DataFrame
print (df)

   price amount revenue
0 22 3 66
1 20 1 20
2 25 3 75
3 30 3 90
4 4 2 8
5 8 4 32
6 12 3 36
7 10 5 50

ဝင်ငွေ ကော်လံအသစ်ရှိ တန်ဖိုးများသည် စျေးနှုန်း နှင့် ပမာဏ ကော်လံများတွင် တန်ဖိုးများဖြစ်ကြောင်း သတိပြုပါ။

ဥပမာ 2- အခြေအနေပေါ်မူတည်၍ ကော်လံနှစ်ခုကို ပွားပါ။

ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' price ': [22, 20, 25, 30, 4, 8, 12, 10],
                   ' amount ': [3, 1, 3, 3, 2, 4, 3, 5],
                   ' type ': ['Sale', 'Refund', 'Sale', 'Sale',
                            'Sale', 'Refund', 'Refund', 'Sale']})

#view DataFrame
print (df)

   price amount type
0 22 3 Dirty
1 20 1 Refund
2 25 3 Dirty
3 30 3 Dirty
4 4 2 Dirty
5 8 4 Refund
6 12 3 Return
7 10 5 Dirty

ကျွန်ုပ်တို့သည် စျေးနှုန်း နှင့် ပမာဏကို ကော်လံများကို ပေါင်း၍ ပွားနိုင်ပြီး၊ ကော်လံ အမျိုးအစား ၏ တန်ဖိုးအပေါ် အခြေခံ၍ ရလဒ်များကို ပြောင်းလဲရန် where () လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။

 #multiply price and amount columns
income = df. price * df. amount

#update values based on type
df[' revenue '] = revenue. where (df. type == ' Sale ', other= 0 )

#view updated DataFrame
print (df)

   price amount type revenue
0 22 3 Dirty 66
1 20 1 Refund 0
2 25 3 Dirty 75
3 30 3 Dirty 90
4 4 2 Dirty 8
5 8 4 Refund 0
6 12 3 Refund 0
7 10 5 Dirty 50

ဝင်ငွေ ကော်လံသည် အောက်ပါတန်ဖိုးများကို ယူသည်ကို သတိပြုပါ။

  • အမျိုးအစား “ ရောင်းရန်” နှင့် ညီမျှပါက စျေးနှုန်းနှင့် ပမာဏ
  • ၀ယ်တာ မဟုတ်လား။

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

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

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

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

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