Pandas- inf ကို max value ဖြင့် အစားထိုးနည်း
pandas DataFrame တွင် အများဆုံးတန်ဖိုးဖြင့် inf နှင့် -inf တန်ဖိုးများကို အစားထိုးရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-
နည်းလမ်း 1- ကော်လံတစ်ခုရှိ အမြင့်ဆုံးတန်ဖိုးဖြင့် inf ကို အစားထိုးပါ။
#find max value of column max_value = np. nanmax (df[' my_column '][df[' my_column '] != np. inf ]) #replace inf and -inf in column with max value of column df[' my_column ']. replace ([np. inf , -np. inf ], max_value, inplace= True )
နည်းလမ်း 2- ကော်လံအားလုံးရှိ အမြင့်ဆုံးတန်ဖိုးဖြင့် inf ကို အစားထိုးပါ။
#find max value of entire data frame
max_value = np. nanmax (df[df != np.inf ])
#replace inf and -inf in all columns with max value
df. replace ([np. inf , -np. inf ], max_value, inplace= True )
အောက်ဖော်ပြပါနမူနာများသည် အောက်ပါ pandas DataFrame နှင့် လက်တွေ့တွင် ဤ syntax ကိုမည်သို့အသုံးပြုရမည်ကို ပြသသည် ။
import pandas as pd
import numpy as np
#createDataFrame
df = pd. DataFrame ({' points ': [18, np.inf, 19, np.inf, 14, 11, 20, 28],
' assists ': [5, 7, 7, 9, 12, 9, 9, np.inf],
' rebounds ': [np.inf, 8, 10, 6, 6, -np.inf, 9, 12]})
#view DataFrame
print (df)
points assists rebounds
0 18.0 5.0 lower
1 lower 7.0 8.0
2 19.0 7.0 10.0
3 lower 9.0 6.0
4 14.0 12.0 6.0
5 11.0 9.0 -inf
6 20.0 9.0 9.0
7 28.0 lower 12.0
ဥပမာ 1- inf ကို ကော်လံတစ်ခုရှိ အများဆုံးတန်ဖိုးဖြင့် အစားထိုးပါ။
အောက်ပါကုဒ်သည် bounces ကော်လံရှိ inf နှင့် -inf တန်ဖိုးများကို bounces ကော်လံရှိ အများဆုံးတန်ဖိုးဖြင့် အစားထိုးနည်းကို ပြသသည်-
#find max value of rebounds
max_value = np. nanmax (df[' rebounds '][df[' rebounds '] != np. inf ])
#replace inf and -inf in rebounds with max value of rebounds
df[' rebounds ']. replace ([np. inf , -np. inf ], max_value, inplace= True )
#view updated DataFrame
print (df)
points assists rebounds
0 18.0 5.0 12.0
1 lower 7.0 8.0
2 19.0 7.0 10.0
3 lower 9.0 6.0
4 14.0 12.0 6.0
5 11.0 9.0 12.0
6 20.0 9.0 9.0
7 28.0 lower 12.0
bounces ကော်လံရှိ inf နှင့် -inf တန်ဖိုးတစ်ခုစီကို ထိုကော်လံတွင် အများဆုံးတန်ဖိုးဖြင့် အစားထိုးထားသည်ကို သတိပြုပါ။
ဥပမာ 2- inf ကို ကော်လံအားလုံးတွင် အများဆုံးတန်ဖိုးဖြင့် အစားထိုးပါ။
အောက်ပါကုဒ်သည် ဒေတာဘောင်တစ်ခုလုံး၏ အများဆုံးတန်ဖိုးဖြင့် ကော်လံတစ်ခုစီ၏ inf နှင့် -inf တန်ဖိုးများကို မည်သို့အစားထိုးရမည်ကို ပြသသည်-
#find max value of entire data frame
max_value = np. nanmax (df[df != np.inf ])
#replace all inf and -inf with max value
df. replace ([np. inf , -np. inf ], max_value, inplace= True )
#view updated DataFrame
print (df)
points assists rebounds
0 18.0 5.0 28.0
1 28.0 7.0 8.0
2 19.0 7.0 10.0
3 28.0 9.0 6.0
4 14.0 12.0 6.0
5 11.0 9.0 28.0
6 20.0 9.0 9.0
7 28.0 28.0 12.0
ကော်လံတစ်ခုစီရှိ inf နှင့် -inf တန်ဖိုးတစ်ခုစီကို ဒေတာဘောင် 28 လုံးရှိ အများဆုံးတန်ဖိုးဖြင့် အစားထိုးထားသည်ကို သတိပြုပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
ပန်ဒါများတွင် ပျောက်ဆုံးနေသောတန်ဖိုးများကို မည်သို့စွပ်စွဲမည်နည်း။
ပန်ဒါမှာ ပျောက်ဆုံးနေတဲ့ တန်ဖိုးတွေကို ဘယ်လိုရေတွက်မလဲ။
ပန်ဒါများတွင် NaN တန်ဖိုးများကို အဓိပ္ပါယ်ဖြည့်နည်း