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


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

နည်းလမ်း 1- Single Column ကို Float အဖြစ်ပြောင်းပါ။

 #convert "assists" column from string to float
df[' assists '] = df[' assists ']. astype (float)

နည်းလမ်း 2- ကော်လံအများအပြားကို Float အဖြစ်ပြောင်းပါ။

 #convert both "assists" and "rebounds" from strings to floats
df[[' assists ', ' rebounds ']] = df[[' assists ', ' rebounds ']]. astype (float)

နည်းလမ်း 3- ကော်လံအားလုံးကို Float အဖြစ်ပြောင်းပါ။

 #convert all columns to float
df = df. astype (float)

အောက်ဖော်ပြပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် လက်တွေ့တွင် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-

 import numpy as np
import pandas as pd

#createDataFrame
df = pd. DataFrame ({' points ': [np.nan, 12, 15, 14, 19],
                   ' assists ': ['5', np.nan, '7', '9', '12'],
                   ' rebounds ': ['11', '8', '10', '6', '6']})  

#view DataFrame
df

 	points assists rebounds
0 NaN 5.0 11
1 12.0 NaN 8
2 15.0 7.0 10
3 14.0 9.0 6
4 19.0 12.0 6

#view column data types
df. dtypes

float64 points
assists object
rebound object
dtype:object

ဥပမာ 1- ကော်လံတစ်ခုတည်းကို Float အဖြစ်ပြောင်းပါ။

အောက်ပါ syntax သည် helper ကော်လံကို string တစ်ခုမှ float သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-

 #convert "assists" from string to float
df[' assists '] = df[' assists ']. astype (float)

#view column data types
df. dtypes

float64 points
assist float64
rebound object
dtype:object

ဥပမာ 2- ကော်လံအများအပြားကို float အဖြစ်ပြောင်းပါ။

အောက်ပါ syntax သည် helper နှင့် ကော်လံ များကို strings မှ float အဖြစ်သို့ ပြောင်းနည်းကို ပြသည်-

 #convert both "assists" and "rebounds" from strings to floats
df[[' assists ', ' rebounds ']] = df[[' assists ', ' rebounds ']]. astype (float)

#view column data types
df. dtypes

float64 points
assist float64
rebounds float64
dtype:object

ဥပမာ 3- ကော်လံအားလုံးကို float အဖြစ်ပြောင်းပါ။

အောက်ပါ syntax သည် DataFrame ရှိ ကော်လံအားလုံးကို float အဖြစ်သို့ ပြောင်းနည်းကို ပြသည်-

 #convert all columns to float
df = df. astype (float)

#view column data types
df. dtypes

float64 points
assist float64
rebounds float64
dtype:object

အပိုဆု- string ကို float နှင့် pad NaN တန်ဖိုးများသို့ ပြောင်းပါ။

အောက်ပါ syntax သည် helper ကော်လံကို string မှ float သို့ ပြောင်းနည်းနှင့် NaN တန်ဖိုးများကို သုညဖြင့် တပြိုင်နက် pad လုပ်ပုံကို ပြသည်-

 #convert "assists" from string to float and fill in NaN values with zeros
df[' assists '] = df[' assists ']. astype (float). fillna (0)

#view DataFrame
df

        points assists rebounds
0 NaN 5.0 11
1 12.0 0.0 8
2 15.0 7.0 10
3 14.0 9.0 6
4 19.0 12.0 6

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

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

Pandas- အရာဝတ္ထုတစ်ခုအား ကိန်းပြည့်အဖြစ်သို့ မည်သို့ပြောင်းလဲမည်နည်း။
ပန်ဒါများ- float များကို ကိန်းပြည့်အဖြစ်သို့ ပြောင်းလဲနည်း
Pandas- သတ်မှတ်ထားသောကော်လံများကို NumPy အခင်းအကျင်းသို့ မည်သို့ပြောင်းရမည်နည်း။

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

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