पांडा: inf को अधिकतम मान से कैसे बदलें


आप पांडा डेटाफ़्रेम में 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 )

निम्नलिखित उदाहरण दिखाते हैं कि निम्नलिखित पांडा डेटाफ़्रेम के साथ व्यवहार में इस सिंटैक्स का उपयोग कैसे करें:

 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 को किसी कॉलम में अधिकतम मान से बदलें

निम्नलिखित कोड दिखाता है कि बाउंस कॉलम में inf और -inf मानों को बाउंस कॉलम में अधिकतम मान से कैसे बदला जाए:

 #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

ध्यान दें कि बाउंस कॉलम में प्रत्येक inf और -inf मान को 12 के उस कॉलम में अधिकतम मान से बदल दिया गया है।

उदाहरण 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 मानों को माध्य से कैसे भरें

एक टिप्पणी जोड़ने

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *